Deployd - 通过AngularJS CORS检索的数据 [英] Deployd - Data retrieved via AngularJS CORS

查看:336
本文介绍了Deployd - 通过AngularJS CORS检索的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通读临AngularJS由亚当·弗里曼。在通过实例去,他有读者创建一个使用角(当然)与Deployd服务器资源体育用品店的应用程序。该Deployd资源设置为返回JSON数据要被填充到模型中。我使用的NodeJS运行我的服务器。它是目前在端口设置5000(的http://本地主机:5000 / sportsstore / app.html )。该Deployd资源在端口5500上运行(的http://本地主机:5500 /产品)。当击中Deployd,响应如下:

I am currently reading through "Pro AngularJS" by Adam Freeman. In going through the examples, he has the reader create a sports store app using Angular (of course) with a Deployd server resource. The Deployd resource is set up to return JSON data that is to be populated into the model. I am using NodeJS to run my server. It is currently setup on port 5000 (http://localhost:5000/sportsstore/app.html). The Deployd resource is running on port 5500 (http://localhost:5500/products). When hitting Deployd, the response is as follows:

[
    { "name": "Kayak", "description": "A boat for one person", "category": "Watersports", "price": 275, "id": "a1c999fc248b2959" },
    { "name": "Lifejacket", "description": "Protective and fashionable", "category": "Watersports", "price": 48.95, "id": "61303717cfad182e" },
    { "name": "Soccer Ball", "description": "FIFA-approved size and weight", "category": "Soccer", "price": 19.5, "id": "0fb5f67bdcbd992f" },
    { "name": "Corner Flags", "description": "Give your playing field a professional touch", "category": "Soccer", "price": 34.95, "id": "24385d315dd388b4" },
    { "name": "Stadium", "description": "Flat-packed 35,000-seat stadium", "category": "Soccer", "price": 79500, "id": "500fb6805905a856" },
    { "name": "Thinking Cap", "description": "Improve your brain efficiency by 75%", "category": "Chess", "price": 16, "id": "637d8a1f42e6fa1c" },
    { "name": "Unsteady Chair", "description": "Secretly give your opponent a disadvantage", "category": "Chess", "price": 29.95, "id": "73393312ec7dfab7" },
    { "name": "Human Chess Board", "description": "A fun game for the family", "category": "Chess", "price": 75, "id": "7871d02a662b0915" },
    { "name": "Bling-Bling King", "description": "Gold plated, diamon-studded King", "category": "Chess", "price": 1200, "id": "b59a3389a0e248bd" }
]

我试图通过使用来获取这些数据 $ http.get

$http.get("http://localhost:5500/products")
    .success(function (data) { ... })
    .error(function (error) { ... });

然而,这使返回一个错误:

However, this keeps returning an error:

XMLHttpRequest cannot load http://localhost:5500/products. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5000' is therefore not allowed access. 

研究表明,有/均一些问题与角和CORS,并且该标头必须被配置为运行跨域请求。因此,我增加了以下到我的app.config:

Research shows that there are/were some issues with Angular and CORS, and that the headers had to be configured to run cross-domain requests. As a result, I added the following to my app.config:

$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With']; // this isn't needed anymore, but was put here as a just-in-case

尽管有添加这些设置,我仍然得到错误。该Deployd文件说,它会自动配置为CORS(跨域请求),并会派只要请求不包含无效的自定义页眉适当的头信息。我是pretty相信我的请求不包含无效的自定义页眉:

Despite having these settings added, I am still getting the error. The Deployd documentation says that it is automatically configured for CORS (Cross-Origin Requests) and will send the appropriate header information as long as the request did not contain invalid custom headers. I'm pretty sure my request does not contain invalid custom headers:

Accept: application/json, text/plain, */*
Cache-Control: max-age=0
Origin: http://localhost:5000
Referer: http://localhost:5000/sportsstore/app.html
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36

我的问题:是否有一些其他的配置,我需要落实到位,才能有Deployd配置为允许CORS请求来处理?这本书没有指定任何特殊角头设置,或其他任何东西。

推荐答案

比克,您deployd的版本升级至0.6.10版本。这样做有我。我现在能够处理一个GET请求。它似乎并不像它与AngularJS code和亚当·弗里曼的书错误。

Bic, upgrade your version of deployd to version 0.6.10. This did the trick for me. I was now able to process a get request. It doesn't seem like it's an error with AngularJS code nor Adam Freeman's book.

在这本书中,他也提到他包括与源$ C ​​$ C在 HTTP下载deployd程序:/ /www.a$p$pss.com/9781430264484 。这是0.6.9版本。我敢肯定,它的工作原理与它的罚款。它会更容易,而不是试图找到0.6.10版本..这是我做的。如果您想要的版本,这里是:

In the book, he does mention that he includes the deployd program with the source code download on http://www.apress.com/9781430264484. That's version 0.6.9. I'm sure it works fine with it. It'll be easier than to try to find the 0.6.10 version.. which is what I did. Should you want that version, here it is:

<一个href=\"https://www.versioneye.com/nodejs/deployd/0.6.10\">https://www.versioneye.com/nodejs/deployd/0.6.10

这不是一个安装程序,所以你必须将它粘贴到deployd目录,替换 node_modules

It's not an installer so you'll have to paste it in your deployd directory, replacing the node_modules.

这篇关于Deployd - 通过AngularJS CORS检索的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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