javascript - Angular 文件上传跨域问题?
天蓬老师
天蓬老师 2017-04-11 12:35:34
[JavaScript讨论组]
  • 使用的是angular-file-upload

  • angular页面将图片上传到图片服务器,出现了跨域问题

//angular页面所在的域名是:http://dev.1-1dr.com
var testObj = angular.module('testApp',['angularFileUpload']);
testObj.controller("testCtr",function($scope,$upload){
    $scope.title = "测试文件上传";
        $scope.fileInfo = file;
        $upload.upload({
            //服务端接收
            url: 'http://upload.1-1dr.com/fileupload',
            //上传的同时带的参数
            data: { 'username': 'test'},
            file: file
        }).success(function (data) {
            //上传成功
            console.info(JSON.stringify(data));
        }).error(function (data, status, headers, config) {
            //上传失败
            console.log('error status: ' + status);
        });
    }
});
XMLHttpRequest cannot load http://upload.1-1dr.com/fileupload. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.1-1dr.com' is therefore not allowed access.
  • 按照CORS的解决方法,服务器端响应添加了Access-Control-Allow-Origin头,但问题还是没解决。

  • 想问,是不是angular在上传文件的请求中,也需要带一些参数,才能解决跨域问题?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(1)
巴扎黑

没有那么一回事,我认为还是你后端在设置CORS没有把你相应的域名写上,或者直接 * 来表示任何域名都可以。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号