【问题标题】:Postman GET request works but not Ajax CORS邮递员 GET 请求有效,但 Ajax CORS 无效
【发布时间】:2018-03-29 14:53:12
【问题描述】:

我正在尝试发出 AJAX 请求以获取 restAPI 的信息。

如果我使用 POSTMAN 对其进行测试,它也适用于 Putty,此代码选择 RAW,但由于 CORS 问题,不适用于 AJAX。

这是适用于 Putty 的代码(它返回 JSON 数据):

GET /api/slot/0/io/do HTTP/1.1\r\n
Host: 172.25.0.208\r\n
Content-Type: application/json\r\n
Accept: vdn.dac.v1\r\n
\r\n

这是 jQuery AJAX 代码:

//Build GET Request URL
var url = "//" + ipDevice + "/api/slot/" + slot + "/io/do";
jQuery.support.cors = true;
//GET Request with HTTP header info
$.ajax({
    "url": url,
    "method": "GET",
    "headers": {
        "Accept": "vdn.dac.v1",
        "Content-type": "application/json"
    },
    "success": function (response) {
        getPowerStatusSuccess(response);
    },
    "error": function (response) {
        getPowerStatusFail(response);
    }
});

我在浏览器控制台 (Firefox) 上遇到的错误是:

【问题讨论】:

标签: javascript jquery ajax cors


【解决方案1】:

您的 api 需要返回一个 cors 标头 ~allow-origin 设置为 * 或域白名单。

access-control-allow-origin: *

【讨论】:

  • 感谢您的回答,但它不是我的 API,所以我无法添加此标头。
猜你喜欢
  • 2021-01-14
  • 2016-12-11
  • 2023-03-14
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-07-26
  • 1970-01-01
  • 2020-09-27
相关资源
最近更新 更多