我目前正在阅读亚当弗里曼的“亲AngularJS”。在阅读这些例子的过程中,他让读者用一个Deployd服务器资源创建了一个使用角(当然)的体育商店应用程序。Deployd资源被设置为返回要填充到模型中的JSON数据。我正在使用NodeJS运行我的服务器。它目前安装在端口5000 (http://localhost:5000/sportsstore/app.html)上。Deployd资源运行在端口5500 (http://localhost:5500/products)上。当点击Deployd时,响应如下:
[
{ "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) { ... });但是,这会继续返回一个错误:
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中:
$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 (Cross-Origin Requests)自动配置的,只要请求不包含无效的自定义标头,就会发送适当的标头信息。我确信我的请求不包含无效的自定义标题:
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请求处理,还需要进行其他配置吗?本书没有指定任何特殊的角标头设置或其他任何内容。
发布于 2014-05-02 18:23:43
Bic,将您的部署升级到0.6.10版本。这对我起了作用。我现在能够处理get请求了。这似乎不是AngularJS代码的错误,也不是Adam的书。
在这本书中,他确实提到他在http://www.apress.com/9781430264484上包含了带有源代码下载的部署程序。这是0.6.9版。我敢肯定它的效果很好。要想找到0.6.10版本要容易得多。我就是这么做的。如果你想要那个版本,就在这里:
https://www.versioneye.com/nodejs/deployd/0.6.10
它不是安装程序,所以您必须将其粘贴到您的部署目录中,替换node_modules。
发布于 2014-05-08 21:24:34
只需在部署安装文件夹中运行"npm“,它将确保更新到最新版本0.6.10。这解决了我阅读了javaauthority的答案后的问题(感谢您的回答:)。
发布于 2014-05-02 08:27:42
你可以把你的文件(app.html,sportStore.js,.)在部署项目的公用文件夹下,并使用以下URL http://localhost:5500/app.html
https://stackoverflow.com/questions/23346863
复制相似问题