简体   繁体   中英

Postman GET request works but not Ajax CORS

I am trying to make an AJAX request to get information of a 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.

This is the code that works on Putty (it returns JSON data):

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:

//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:

图片

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

access-control-allow-origin: *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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