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标头是否有效
- PUT发送数据
第一个失败,HTTP 404 Not Found。 CORS repsonse标头看起来有效:
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
在我的节点环境的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'); res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With, Origin, Accept'); res.header('Access-Control-Allow-Credentials', 'true'); next(); });
和
var me = require('./me'); app.get('/me', loadUser, me.show); app.put('/me', loadUser, me.update);
从子域获取数据是没有问题只发送任何想法我忘记了什么?
此致,博多
试试这个:
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同步请求无法在Firefox中运行
- Cookie不是通过跨域AJAX请求设置的
- jQuery AJAX调用导致错误状态403
- XMLHttpRequest无法加载file:/// C:/Users/hamma/Desktop/rao.html
- 这里有什么问题让IE9可以做CORS Ajax调用,但IE8和FireFox不能?
- 如何使用Access-Control-Allow-Origin:https://www.example.com?
- CORS无法在Chrome上运行
- CORS + Cordova:问题:Access-Control-Allow-Origin
- 当API不允许Access-Control-Allow-Origin时该怎么办