CORS PUT:在jQuery.ajax中找不到 [英] CORS PUT: Not found with jQuery.ajax
本文介绍了CORS PUT:在jQuery.ajax中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向我的子域发送一些原始数据:
$。ajax({
url :http://accounts.example.com:3000/me,
type:PUT,
data:{wabo:chabo},
dataType:json ,
xhrFields:{
withCredentials:true
}
});
浏览器现在发送两个请求:
- 对accounts.example.com:3000/me的选项检查CORS标头是否有效
- 输入发送数据
第一个失败,HTTP 404未找到。
CORS repsonse标头看起来有效:
HTTP / 1.1 404未找到
/ pre>
X-Powered- :Express
Access-Control-Allow-Origin:http://node.example.com:3000
Access-Control-Allow-Methods:HEAD,GET,POST,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers:Content-Type,X-Requested-With,Origin,Accept
Access-Control-Allow-Credentials:true
Content-Type:text / plain
Set-Cookie:connect.sid = s%3Aru8njoU2ZAnoKL2W2w%2B0BHF7。%2Fe%2FQI5f6NKRWQvWlcYEkMG7HHSxxj0haFDBUID2g45A; Domain = .example.com; Path = /
日期:Sun,04 Nov 2012 11:31:22 GMT
连接:keep-alive
传输编码:分块
在我的节点环境的app.js中,我有:
app.all('*',function(req,res,next){
res.header('Access-Control-Allow-Origin','http://node.example.com:3000' );
res.header('Access-Control-Allow-Methods','HEAD,GET,POST,PUT,DELETE,OPTIONS' ('Access-Control-Allow-Credentials','true');
next());
res.header ;
});
和
var me = require('./ me');
app.get('/ me',loadUser,me.show);
app.put('/ me',loadUser,me.update);
从子域获取数据没有问题,只发送
任何想法我忘记了什么?
请注意,bodo
解决方案>
app.all('*',function(req,res,next){
res.header -Allow-Origin','http://node.example.com:3000');
res.header('Access-Control-Allow-Methods','HEAD,GET,POST,PUT,DELETE, ('Access-Control-Allow-Headers','Content-Type,X-Requested-With,Origin,Accept');
res.header控制允许证书','true');
if(req.method.toLowerCase()===options){
res.send(200);
}
else {
next();
}
});
I want to send some raw data to a subdomain of mine:
$.ajax({ url: "http://accounts.example.com:3000/me", type: "PUT", data: { wabo: "chabo" }, dataType: "json", xhrFields: { withCredentials: true } });
The browser now sends two requests:
- OPTIONS to accounts.example.com:3000/me to check if CORS headers are valid
- PUT to send data
The first one fails with HTTP 404 Not Found. The CORS repsonse header looks valid:
HTTP/1.1 404 Not Found X-Powered-By: Express Access-Control-Allow-Origin: http://node.example.com:3000 Access-Control-Allow-Methods: HEAD, GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Headers: Content-Type, X-Requested-With, Origin, Accept Access-Control-Allow-Credentials: true Content-Type: text/plain Set-Cookie: connect.sid=s%3Aru8njoU2ZAnoKL2W2w%2B0BHF7.%2Fe%2FQI5f6NKRWQvWlcYEkMG7HHSxxj0haFDBUID2g45A; Domain=.example.com; Path=/ Date: Sun, 04 Nov 2012 11:31:22 GMT Connection: keep-alive Transfer-Encoding: chunked
In app.js of my node environment I have:
app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', 'http://node.example.com:3000'); res.header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Origin, Accept'); res.header('Access-Control-Allow-Credentials', 'true'); next(); });
and
var me = require('./me'); app.get('/me', loadUser, me.show); app.put('/me', loadUser, me.update);
GETting data from the subdomain is no problem only sending Any idea what I have forgotten?
Regards, bodo
解决方案Try this instead:
app.all('*', function(req, res, next) { res.header('Access-Control-Allow-Origin', 'http://node.example.com:3000'); res.header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS'); res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Origin, Accept'); res.header('Access-Control-Allow-Credentials', 'true'); if( req.method.toLowerCase() === "options" ) { res.send( 200 ); } else { next(); } });
这篇关于CORS PUT:在jQuery.ajax中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文