简体   繁体   English

Postman GET 请求有效,但 Ajax CORS 无效

[英]Postman GET request works but not Ajax CORS

I am trying to make an AJAX request to get information of a restAPI.我正在尝试发出 AJAX 请求以获取 restAPI 的信息。

If I test it with POSTMAN, it works and with Putty too with this code selecting RAW, but not with AJAX due to CORS issues.如果我使用 POSTMAN 对其进行测试,它也可以使用 Putty 进行测试,此代码选择 RAW,但由于 CORS 问题无法使用 AJAX。

This is the code that works on Putty (it returns JSON data):这是适用于 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

This is the jQuery AJAX code:这是 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);
    }
});

The error I got on browser console (Firefox) is:我在浏览器控制台(Firefox)上遇到的错误是:

图片

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

access-control-allow-origin: *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM