【问题标题】:CORS PUT: Not found with jQuery.ajaxCORS PUT:在 jQuery.ajax 中找不到
【发布时间】:2012-10-24 11:11:35
【问题描述】:

我想将一些原始数据发送到我的子域:

$.ajax({
    url: "http://accounts.example.com:3000/me",
    type: "PUT",
    data: { wabo: "chabo" },
    dataType: "json",
    xhrFields: {
        withCredentials: true
    }
});

浏览器现在发送两个请求:

  1. accounts.example.com:3000/me 的选项以检查 CORS 标头是否有效
  2. PUT 发送数据

第一个失败,HTTP 404 Not Found。 CORS 响应标头看起来有效:

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);

从子域获取数据只是发送没问题 知道我忘记了什么吗?

问候,博多

【问题讨论】:

    标签: javascript node.js http jquery cors


    【解决方案1】:

    试试这个:

    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();
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 2012-07-28
      • 2015-11-19
      • 2015-02-13
      • 2017-07-16
      • 2020-06-23
      • 2011-07-16
      • 2015-08-13
      • 2012-09-19
      相关资源
      最近更新 更多