0

I am working website and hitting a third party api but getting an error No 'Access-Control-Allow-Origin' header is present on the requested resource

Ajax:

var headers = new Headers();
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
    headers.append('Accept', 'application/json');
    headers.append('content-type', 'application/json');
    headers.append('AuthToken', '2948f47085e9d8ecd95bd21ebe024a01516105f9')
    headers.append('Access-Control-Allow-Origin', 'http://localhost:61030/Test.aspx');
    headers.append('Access-Control-Allow-Credentials', 'true');

    $.ajax({
        crossDomain: true,
        type: "GET",
        url: "https://api.travelcloudpro.eu/v1/cache/flyfrom?origin=DEL&pointOfSale=US",
        //headers: { '[{"key":"AuthToken","value":"2948f47085e9d8ecd95bd21ebe024a01516105f9","description":""}]': 'some value' },
        //  header: { 'AuthToken': '2948f47085e9d8ecd95bd21ebe024a01516105f9' },
        header:headers,
        dataType: "json",
        data:{},
        success: function (data) {
            debugger;
            var flightresponse = data;

            //alert($("#city").val());
        }
    });

While calling from postman it works fine.

enter image description here

I don't know what is missing, TIA.

if i change datatype json to jsonp then getting this error :- enter image description here

  • You're using GET in postman... – Marcus Höglund Dec 14 '17 at 10:52
  • 2
    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) – Divyang Desai Dec 14 '17 at 12:01
  • 2
    Possible duplicate of [No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API](https://stackoverflow.com/questions/43871637/no-access-control-allow-origin-header-is-present-on-the-requested-resource-whe) – CodeCaster Dec 14 '17 at 12:01
  • Read [ask] and share your research. Adding random response headers as request headers isn't going to solve the issue. – CodeCaster Dec 14 '17 at 12:02

1 Answers1

0

i guess as you mentioned "third party api" you are sending request to a server with different ip address (or port number) which makes a CORS error(Cross-Origin Resource Sharing) in most browsers like firefox and chrome.

+here you can read more about why it happens.+

you need to add a CORS Filter which is discussed +here+.

I`m not a .net programmer but i hope it could help.