Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';不要得到AngularJS';s$http用于执行跨域请求,如jQuery';s";ajax";_Javascript_Jquery_Angularjs_Xmlhttprequest_Cors - Fatal编程技术网

Javascript Can';不要得到AngularJS';s$http用于执行跨域请求,如jQuery';s";ajax";

Javascript Can';不要得到AngularJS';s$http用于执行跨域请求,如jQuery';s";ajax";,javascript,jquery,angularjs,xmlhttprequest,cors,Javascript,Jquery,Angularjs,Xmlhttprequest,Cors,我需要使用来自第三方的API,他们向我发送了一个测试html文件供我使用。我在浏览器中像这样打开文件:file//…,它可以通过按下一些触发jQuery代码的按钮成功地执行请求。他们使用的jQuery如下: $.ajaxSetup({ xhrFields: { withCredentials: true }, crossDomain: true, }); $.ajax({ beforeSend :

我需要使用来自第三方的API,他们向我发送了一个测试
html
文件供我使用。我在浏览器中像这样打开文件:
file//…
,它可以通过按下一些触发
jQuery
代码的按钮成功地执行请求。他们使用的
jQuery
如下:

$.ajaxSetup({
        xhrFields: {
            withCredentials: true
        },
        crossDomain: true,
    });


$.ajax({
        beforeSend : function(xhr){
            xhr.setRequestHeader('X-Requested-With','XMLHttpRequest');
        },
        async:true,
        url        : loginUrl,
        method     : 'post',
        //cache      : false,
        contentType: 'application/x-www-form-urlencoded',
        data       : data,
        processData: true,
        success    : loginHandle,
        error      : loginError
    });
我在AngularJS尝试这个,但运气不好:

...

$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

...

return $http.post(host+'/admin/login', {data: credentials})
    .then(complete)
    .catch(failed);
我得到这个错误:

XMLHttpRequest cannot load http://x.x.x.x:8080/admin/login. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value 'null'. Origin 'http://example.com' is therefore not allowed access.`
他们的
Access Control Allow Origin
被设置为
null
,他们的请求头也被
Origin
设置为
null

我有没有办法让它工作起来,至少用于测试目的?我查看了
$http.jsonp
,但它似乎不支持
POST
数据

编辑:


我只是在我的AngularJS文件中使用了他们的
$.ajax
代码,它工作了,但只使用了下面提到的Chome CORS插件。我猜当
html
文件在浏览器中使用
file://
运行时,原点会自动设置为
null

这最终在Chrome CORS插件中起作用,请注意
$.param
函数

return $http({
            url: host+'/admin/login',
            data: $.param(credentials),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            method: 'POST'
        })
        .then(complete)
        .catch(failed);

该jQuery代码也不应该工作,因为没有设置CORs标头。好吧,它正在工作。jQuery发送的内容类型与角度请求不同,这种差异足以让浏览器发送角度请求的预飞行,而不是jQuery请求的预飞行。也。。。您不需要将
crossDomain
设置为
true
。如果是跨域请求,则默认为true。所有提示它的答案都是胡说八道。您需要在web服务器上打开此文件,而不是在
文件://
protocol@charlietfl我不认为这有什么关系,因为他将请求发送到服务器上的东西,而不是文件系统。