1
votes

I am trying to make a GET request and for this I am supposed to send header values, I Have tried the methods below. This API call seems to be working fine in POSTMAN extension I use to test the API's

       
        $.ajax({
            url: "requested url",
            type: 'GET',
            beforeSend: function (xhr) {
                xhr.setRequestHeader('setHeader', '12345');
            },
            success: function (data) {
                console.log(data);
            },
            error: function () {
                alert("Failed");
            },
        });

AJAX request seems to be working fine, but the moment I add a header via beforeSend or headers, an OPTIONS pre-flight request is made and the GET request is aborted.

I have also tried setting the headers using $.ajaxSetup();


        $.ajaxSetup({
            beforeSend: function (xhr) {
                xhr.setRequestHeader('setHeader', '12345');
            }
        });


        $.ajax({
            type: "GET",
            url: "requested url"
        }).success(function (data) {
            console.log(data);
        }).error(function () {
            alert("Failed");
        });

Request headers :

Request URL:requested url
Request Method:OPTIONS
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, setHeader
Access-Control-Request-Method:GET

Response headers :

Access-Control-Allow-Headers:Content-Type, Accept, X-Requested-With,setHeader
Access-Control-Allow-Method:GET,POST,PUT,OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Expose-Headers:*

I get this error in my console :

XMLHttpRequest cannot load "requested url". Invalid HTTP status code 404

1

1 Answers

-1
votes

Maybe your problem is jsonp related. Just guesing here.

what-is-jsonp-all-about
can-i-set-headers-in-cross-domain-json-requests

Check your server headers aswell.