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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27145287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:47:09  来源:igfitidea点击:

AngularJS - $http get returns error with status 0?

javascriptxmlangularjscorshttp-get

提问by kumareloaded

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(这当通过在浏览器中输入链接手动访问时,会显示 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?

问题是因为我试图从不同的域访问 XML 文件(CORS 问题?)还是其他原因?

Please help me out!

请帮帮我!

回答by KByK

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. 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)

响应您的 REST 请求的服务器必须包含 CORS 指定的标头,以便 Angular 正确使用响应。本质上,这意味着在您的响应中包含 Access-Control-Allow-Origin 标头,指定请求来自的服务器,这是允许的。(参考

There is directive Angularallows one to get/set the whitelists and blacklists used to ensure that the URLs used for sourcing Angular templates are safe.

有指令Angular允许获取/设置用于确保用于采购 Angular 模板的 URL 安全的白名单和黑名单。

回答by Kuldeep Dangi

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. you can find more information on it here

是的,你没看错,因为 $http 的默认内容类型是“application/json”,如果你想改变请求数据或响应数据,你可以在 tranformRequest 和 tranformResponse 的帮助下改变它。你可以在这里找到更多信息

also i find an article on implementation of tranformRequest change post data from json to form post in angularjs

我还找到了一篇关于实现tranformRequest 将 post 数据从 json 更改为 angularjs 中的 form post 的文章