AngularJS - $ HTTP GET返回错误状态为0? [英] AngularJS - $http get returns error with status 0?
问题描述
我在这我想从一个服务器$ HTTP GET XML数据的应用程序AngularJS说的 http://example.com/a/b/c/d/getDetails?fname=abc&lname=def (这时候通过手动访问进入在浏览器中的链接显示了XML文件的树结构)。
I've an AngularJS application in which I'm trying to get an XML data with $http get from a server say http://example.com/a/b/c/d/getDetails?fname=abc&lname=def (this when accessed manually by entering the link in a browser shows a tree structure of the XML file).
当我从该链接运行应用程序的数据并不牵强。相反,它显示一个错误的状态0
When I run the application the data from that link is not fetched. Instead its showing an error with status 0.
//url = http://example.com/a/b/c/d/getDetails?fname=abc&lname=def
$http.get(url).success(function (data){
alert("Success");
deferred.resolve(data);
}).error(function (data, status){
console.log("Error status : " + status);
});
我不知道为什么$ http.get出现故障并以误差函数返回状态0。
I'm not sure why the $http.get fails and goes to the error function returning status 0.
但是,如果我通过一个URL,其中有一个JSON对象的本地服务器,它的工作原理。
But if I pass an URL to the local server which has a JSON object, it works.
是因为我试图访问从不同的域XML文件(CORS问题?),还是别的什么问题?
Is the problem because of me trying to access the XML file from a different domain(CORS issue?) or something else?
请帮我出!
推荐答案
您有 HTTP访问控制(CORS)问题。
回答您的REST请求必须包括CORS指定的头文件,以便服务器允许角正常消耗的响应。从本质上讲,这意味着包括在响应访问控制允许来源头,指定从哪里请求来自,被允许的服务器。 (参考)
The server answering your REST requests MUST include the headers specified by CORS in order to allow Angular to consume properly the response. Essentially this means including the Access-Control-Allow-Origin header in your response, specifying the servers from where the request comes from, that are allowed. (ref)
有是指令角允许一个获取/设置用来保证白名单和黑名单用于采购角模板的URL是安全的。
There is directive Angular allows one to get/set the whitelists and blacklists used to ensure that the URLs used for sourcing Angular templates are safe.
这篇关于AngularJS - $ HTTP GET返回错误状态为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!