-1

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:

Image

Rob
  • 2,243
  • 4
  • 29
  • 40
JoakDA
  • 305
  • 1
  • 13
  • 3
    Possible duplicate of [Why does my JavaScript get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-get-a-no-access-control-allow-origin-header-is-present) – BrTkCa Oct 17 '17 at 19:59

1 Answers1

1

Your api needs to return a cors header ~allow-origin set to * or a whitelist of domains.

access-control-allow-origin: *

Scriptonomy
  • 3,975
  • 1
  • 15
  • 23