javascript - xmlhttprequest CORS 不允许我访问我的 api

标签 javascript ajax cors cgi

我确实对 CORS 有一些问题,我有 2 个网络服务器正在运行,其中一个正在托管实际页面,另一个正在运行一些 python cgi skripts。 我的实际网页所在的页面包含此标题的响应:

一般

Request URL:http://10.208.71.10/de/svg/00000002.svg?base=undefined
Request Method:GET
Status Code:200 OK
Remote Address:10.208.71.10:80

响应 header :

HTTP/1.0 200 OK
Server: WebMI SDK/0.9.0
Date: Tue, 26 Jan 2016 21:33:18 GMT
Connection: Keep-Alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
Content-Length: 2378608
Content-Type: image/svg+xml
Last-Modified: Tue, 26 Jan 2016 21:13:28 GMT
Content-Encoding: gzip

查询字符串参数:

base=undefined

此页面包含一小段代码:

var getJSON = function(url, successHandler, errorHandler) {
  var xhr = typeof XMLHttpRequest != 'undefined'
    ? new XMLHttpRequest()
    : new ActiveXObject('Microsoft.XMLHTTP');
  xhr.open('get', url, true);
  xhr.setRequestHeader('origin', "http://10.208.65.216*");
  xhr.onreadystatechange = function() {
    var status;
    var data;
    // https://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
    if (xhr.readyState == 4) { // `DONE`
      status = xhr.status;
      if (status == 200) {
        data = JSON.parse(xhr.responseText);
        successHandler && successHandler(data);
      } else {
        errorHandler && errorHandler(status);
      }
    }
  };
  xhr.send();
};

当我调用 getJSON("http://10.208.65.216:8082/cgi/3daxis.py ", function(d) {...}, function(s) {...}); 浏览器因 CORS 错误而拒绝它。

我的第二个运行 cgi 脚本的网络服务器将像这样回答(如果浏览器对此感到满意:S):

一般

Request URL:http://10.208.65.216:8082/cgi/3daxis.py
Request Method:GET
Status Code:200 Script output follows
Remote Address:10.208.65.216:8082

响应 header

HTTP/1.0 200 Script output follows
Server: SimpleHTTP/0.6 Python/2.7.3
Date: Tue, 26 Jan 2016 19:35:35 GMT
Content-type: text/plain
Content-Location: 3daxis.py
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Cache-Control: no-cache, no-store, must-revalidate

我做错了什么?

最佳答案

Access-Control-Allow-Methods 不能具有值 *。我建议学习CORS protocol .

关于javascript - xmlhttprequest CORS 不允许我访问我的 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022682/

相关文章:

javascript - SlickGrid 类似 Excel 的行为

javascript - app.js 文件中出现错误,我无法破解

javascript - 使用 JS 更改网站时如何保留范围或携带变量

php - Ajax - 每秒检查数据库表值

JavaScript 图像被 CORS 阻止

java - 使用 FFMPEG 从图像创建视频

javascript - 使用 Ajax Prototype 更新表格中的数据

java - 使用 ajax jquery Struts-1 设置 Action Class 属性

server - 如何使用 Lacinia Pedestal 设置 COR?

java - 请求的资源上存在 GWT-No 'Access-Control-Allow-Origin' header