简体   繁体   English

AngularJS - $ http get返回错误,状态为0?

[英]AngularJS - $http get returns error with status 0?

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). 我有一个AngularJS应用程序,其中我试图从服务器获取带有$ http的XML数据,例如http://example.com/a/b/c/d/getDetails?fname=abc&lname=def(this)通过在浏览器中输入链接手动访问时显示XML文件的树结构)。

When I run the application the data from that link is not fetched. 当我运行应用程序时,不会获取该链接中的数据。 Instead its showing an error with status 0 . 相反,它显示状态为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);
            });

I'm not sure why the $http.get fails and goes to the error function returning status 0. 我不确定为什么$ http.get失败并转到错误函数返回状态0。

But if I pass an URL to the local server which has a JSON object, it works. 但是如果我将URL传递给具有JSON对象的本地服务器,它就可以工作。

Is the problem because of me trying to access the XML file from a different domain(CORS issue?) or something else? 是因为我试图从不同的域(CORS问题?)或其他东西访问XML文件?

Please help me out! 请帮帮我!

You have HTTP access control (CORS) issues . 您有HTTP访问控制(CORS)问题。

The server answering your REST requests MUST include the headers specified by CORS in order to allow Angular to consume properly the response. 响应REST请求的服务器必须包含CORS指定的头,以便Angular正确使用响应。 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. 实质上,这意味着在响应中包含Access-Control-Allow-Origin标头,指定允许来自请求的服务器。 ( ref ) 参考

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. 有一个指令Angular允许用户获取/设置白名单和黑名单,以确保用于采购Angular模板的URL是安全的。

yes you got it right, its because default content-type for $http is “application/json” if you want to change the request data or response data you can change it with the help of tranformRequest and tranformResponse. 是的,你做对了,因为$ http的默认内容类型是“application / json”,如果你想改变请求数据或响应数据,你可以在tranformRequest和tranformResponse的帮助下改变它。 you can find more information on it here 你可以在这里找到更多相关信息

also i find an article on implementation of tranformRequest change post data from json to form post in angularjs 另外,我找到一篇关于tranformRequest实现的文章, 将数据从json发布到angularjs中的post post

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM