使用Node.js的服务器上CORS上PhoneGap的应用 [英] Using CORS on node.js server on a phonegap application
问题描述
我想用我的node.js服务器连接我的科尔多瓦/ PhoneGap的应用程序。问题是,我得到这个错误XMLHtt prequest无法加载http://127.0.0.1:1234/api/users没有访问控制 - 允许 - 原产地标头是$ P $ 。psent所请求的资源产地'的http://本地主机:5000'。因此不会允许访问的响应有HTTP状态code 500
。我尝试添加这样的中间件到我的node.js服务器,进入我的PhoneGap域,那么,只是为了测试,当然效果影响不大,我想我将允许所有域的CORS,仍然没有运气。这是我用我的node.js服务器上:
app.use(功能(REQ,水库,下一个){
//网站要允许连接
res.setHeader(访问控制 - 允许 - 原产地','*');
//你希望允许请求的方法
res.setHeader(访问控制 - 允许 - 方法,GET,POST,OPTIONS,PUT,补丁,删除);
//请求头要允许
res.setHeader(访问控制 - 允许 - 头,X-要求,通过,内容型);
//设置为true,如果你需要的网站,包括在发送的请求的cookie
//对API(例如,如果你使用的会话)
res.setHeader(访问控制 - 允许 - 凭据,真正的);
//传递给中间件的下一层
下一个();
});
和我做从我的PhoneGap的应用程序,我认为这是pretty的很多正确的一个pretty的简单的AJAX请求。我对我的PhoneGap应用程序收到此错误和Node.js的服务器控制台上我从控制器之一的错误,说不能看的定义,其中参数X未定义
是我的 req.body
。这里是要求:
VAR serverURL使用='http://127.0.0.1:1234';
VAR体= {
用户名:123A,
评论: {
来自:123B,
内容:我是新一代的123! :)
}
};
VAR bodyToString = JSON.stringify(身体);
$阿贾克斯(网址:{url:serverURL使用+/ API /板,数据类型:JSON,键入:PUT,数据:bodyToString})来实现(功能(数据){。
警报(数据);
});
您的节点服务器上允许*出身的访问是一个坏主意。 http://docs.phonegap.com/en/1.9.0/guide_whitelist_index .md.html 可以声明所有域在PhoneGap的本身你的应用程序的支持,在cordova.xml使用的元素。看看这个<一href="http://stackoverflow.com/questions/19003025/phonegap-cross-domain-ajax-post-request-not-working-on-android">Phonegap跨域AJAX POST请求不工作在Android
修改
: 从下面的评论:如果您需要允许访问任何起源于Chrome浏览器只是为发展目的,有一个插件<一个href="https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi" rel="nofollow">https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
I am trying to connect my cordova/phonegap application with my node.js server. Problem is, I get this error "XMLHttpRequest cannot load http://127.0.0.1:1234/api/users. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:5000' is therefore not allowed access. The response had HTTP status code 500. "
. I tried adding this middleware to my node.js server, entering my phonegap domain, then, just for testing purpouses of course, I figured I will allow ALL domains for CORS, still no luck. This is what I used on my node.js server:
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
And I am doing a pretty simple AJAX request from my phonegap application, which I think is pretty much correct. I am getting this error on my phonegap app and on the node.js server console I get an error from one of the controllers, saying can't see property X of undefined, which undefined
is my req.body
. Here is the request:
var serverURL = 'http://127.0.0.1:1234';
var body = {
userId : '123A',
comment: {
from: '123B',
content: 'I am a new generation of 123! :)'
}
};
var bodyToString = JSON.stringify(body);
$.ajax({url: serverURL + "/api/plates", dataType: "json", type: "PUT", data: bodyToString}).done(function (data) {
alert(data);
});
Allowing * origin access on your node server is a bad idea. http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html you can declare all domains to be supported by your app in phonegap itself, using element in cordova.xml. have a look at this Phonegap Cross-domain AJAX POST Request not working on Android
Edit
: From comment below: if you need to allow access to any origin from chrome just for development purpose there is a plugin https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
这篇关于使用Node.js的服务器上CORS上PhoneGap的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!