ajax - 从 ExtJS 请求到 node.js 时出现 CORS 问题。请求或响应 header 不正确?

标签 ajax node.js extjs http-headers cors

我在向我们网络中两个不同域之间的 Nodejs 服务器发出 ExtJS AJAX 请求时遇到问题,我将不胜感激任何帮助。从 ExtJS 客户端尝试从 http 和 https 响应失败,但从我的本地通过 http 的 Curl 返回 200 OK 和正确的数据。我们正在处理内容类型 application/json

ExtJS onReady 函数已启用 CORS:

Ext.onReady(function () {
   Ext.Ajax.cors = true;
   Ext.Ajax.useDefaultXhrHeader = false;
   ... (code removed) 
})

从我的 ExtJS 客户端对已知工作 URL 进行的测试将正确创建 ExtJS 数据存储(返回 200 OK):

Ext.create('Ext.data.Store', {
    id         : 'countryStore',
    model      : 'country',
    autoLoad   : true,
    autoDestroy: true,
    proxy: {
        type: 'rest',
        url : 'https://restcountries.eu/rest/v1/all',
        },
        reader: {
            type           : 'json',
            headers: {'Accept': 'application/json'},
            totalProperty  : 'total',
            successProperty: 'success',
            messageProperty: 'message'
        }
});

但是,当尝试通过

向我们的 Nodejs 服务器发出请求时

http:

Ext.create('Ext.data.Store', {
    id         : 'circuits',
    model      : 'circuit',
    autoLoad   : true,
    autoDestroy: true,
    proxy: {
        type: 'rest',
        url : 'http://ourNodeJsServerDomain:5500/v3/circuits',
        },
        reader: {
            type           : 'json',
            headers: {'Accept': 'application/json'},
            totalProperty  : 'total',
            successProperty: 'success',
            messageProperty: 'message'
        }
});

在 Chrome 的控制台中返回以下内容:

混合内容:“https://ourExtJsDevClientSide”页面是通过 HTTPS 加载的,但请求了不安全的 XMLHttpRequest 端点“http://ourNodeJsServerDomain:5500/v3/circuits?_dc=1430149427032&page=1&start=0&limit =50'。此请求已被阻止;内容必须通过 HTTPS 提供。

现在,当尝试通过 https 时:

Firefox 显示:

跨源请求被阻止:同源策略不允许读取位于 https://ourNodeJsServerDomain:5500/v3/circuits?_dc=1430151516741&page=1&start=0&limit=50 的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

并且请求 header 不显示“application/json”,这是一个问题吗?:

Accept  
text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding 
gzip, deflate
Accept-Language 
en-US,en;q=0.5
Host    
ourNodeJsServerDomain:5500
Origin  
https://ourExtJsDevClientSide
Referer 
https://ourExtJsDevClientSide
User-Agent  
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0

然后我尝试使用 Curl 来查看有助于调试的响应

http 上给出 200 OK 响应,但 Access-Control-Allow-Origin 未定义,即使我们将其定义为 "*":

curl http://ourNodeJsServerDomain:5500/v3circuits?_limit=1 -v
> GET /v3/circuits?_limit=1 HTTP/1.1
> User-Agent: curl/7.37.1
> Host: ourNodeJsServerDomain:5500
> Accept: */*
> 
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Access-Control-Allow-Origin: undefined
< Access-Control-Allow-Methods: GET
< Access-Control-Allow-Headers: Content-Type
< Content-Type: application/json; charset=utf-8
< Content-Length: 1126
< ETag: W/"MlbRIlFPCV6w7+PmPoVYiA=="
< Date: Mon, 27 Apr 2015 16:24:18 GMT
< Connection: keep-alive
< 
[
  { *good json data returned here* } ]

然后当我尝试通过 https

进行 curl 时
curl https://ourNodeJsServerDomain:5500/v3/circuits?_limit=1 -v
* Server aborted the SSL handshake
* Closing connection 0
curl: (35) Server aborted the SSL handshake

我们在 Nodejs 服务器上启用了 CORS:

router
    .all('*', function(req, res, next){
        res.setHeader('Content-Type', 'application/json');
        // res.setHeader("Access-Control-Allow-Origin", req.headers.origin);
        // console.log('\n\nreq.headers.origin ===================== ' + req.headers.origin);
        //I have tried allowing all * via res.SetHeader and res.header and neither is defining the Access-Control-Allow-Origin properly when curling
        //res.setHeader("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Origin", "*");
        // res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header('Access-Control-Allow-Methods', 'GET');
        res.header('Access-Control-Allow-Headers', 'Content-Type');

我已经尝试详细说明我的思考过程,我愿意尝试新的方法来确定如何理解和解决这个问题。

* 解决方案 *

问题是来自浏览器的混合内容。我们的客户端 UI 在 https(安全)上,而我们从 nodejs 服务器请求 http(不安全)内容。我们需要允许我们的 nodejs 服务器在 https 上运行

我们generated SSL certifications并将它们实现到我们的 nodejs 服务器上。

在 nodejs 代码中,我们使用 CORS 模块启用了 CORS,并同时运行 http 和 https 服务器:

// enable CORS for all requests
var cors = require('cors');
app.use(cors());

// for certifications
var credentials = {
  key: fs.readFileSync('our.key'),
  cert: fs.readFileSync('our.crt')
};

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);

httpServer.listen(port, function() {
console.log('HTTP server listening on port ' + port);
});

httpsServer.listen(httpsPort, function() {
    console.log('HTTPS server listening on port ' + httpsPort);
});

最佳答案

您的服务器中的 CORS 和 HTTPS 似乎都存在问题...您应该尝试 this middleware对于 CORS 部分,并在首先以原始 HTTP 访问页面时使其工作。据我所知,你必须use different ports用于 HTTP 和 HTTPS。您可能还需要启用 CORS credentials .正如我所说,我认为你最好先让它在 HTTP 中工作;)

然后,在 Ext 部分,正如评论中已经提到的那样,您应该禁用默认 header (或者您必须让服务器接受所有这些 header ;请参阅 this answer 的第一条评论)。但是你需要在代理上这样做,因为apparently it replaces the global settingExt.Ajax 中。

所以,像这样:

Ext.create('Ext.data.Store', {
    id         : 'countryStore',
    model      : 'country',
    autoLoad   : true,
    autoDestroy: true,
    proxy: {
        type: 'rest',
        url : 'https://restcountries.eu/rest/v1/all',
        useDefaultXhrHeader: false, // <= HERE
        reader: {
            type           : 'json',
            headers: {'Accept': 'application/json'},
            totalProperty  : 'total',
            successProperty: 'success',
            messageProperty: 'message'
        }
    } // <= and notice this change
});

可能不相关,但请注意,您的缩进不正确并隐藏了 reader 选项应用于商店本身而不是代理(因此被忽略)这一事实。

关于ajax - 从 ExtJS 请求到 node.js 时出现 CORS 问题。请求或响应 header 不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29901581/

相关文章:

extjs - 学习 ExtJS4

javascript - 将 Net 日期时间格式转换为 JavaScript 在 IE 上不起作用

javascript - 由于 xmlhttp.send(NULL);,txt 中的计数被重置!

php - 在同一个 vps 上运行 node、PHP 和 Python

node.js - 如何伪造直接需要的函数

javascript - 使用 Extjs3 在组合框中动态加载 json 数据

javascript - 在 tampermonkey 中,变量 urlm 的行为有所不同

javascript - 禁用 AJAX 请求直到完成?

javascript - 下面的代码块在我的异步等待完成之前执行

javascript - 如何将 cls 放入 tagfield 编辑器中