AngularJS - $http get 返回状态为 0 的错误? [英] AngularJS - $http get returns error with status 0?

查看:26
本文介绍了AngularJS - $http get 返回状态为 0 的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AngularJS 应用程序,我试图在其中使用 $http 从服务器获取 XML 数据,例如 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 指定的标头,以便 Angular 正确使用响应.本质上,这意味着在您的响应中包含 Access-Control-Allow-Origin 标头,指定请求来自的服务器,这是允许的.(参考)

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)

有指令 Angular 允许获取/设置白名单和用于确保用于采购 Angular 模板的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆