0

I have a function that call web services API.

     function GetData() {         
        var prm =
         {
             EVENT_CODE: EvnCode
         };
        $.ajax({
            url: 'http://00.00.00.000/CRM/geteventwisebudgetactualamount',
            headers: {
                'AuthKey': 'test',
                'Content-Type': 'application/x-www-form-urlencoded',                    
            },            

            type: 'POST',
            contentType: 'application/x-www-form-urlencoded',
            data: prm,
            crossDomain: true,
            dataType: 'json',
            success: function (data) {


            },
            error: function (errMsg) {
                console.log('AJAX FAILED, message : ' + errMsg);
            }
        });
    }

it always show the error mention in title, Please help to resolve this issue.

peeebeee
  • 2,541
  • 6
  • 21
  • 26
Sumit k
  • 75
  • 7
  • This answer explains CORS well - https://stackoverflow.com/a/10636765/3574481 – peeebeee Aug 31 '18 at 06:17
  • i am little bit confused, i got this api url from other agency, now i don't know what are they doing in their code. i am just using their api url to fetch the record using ajax.Please suggest, this problem occur from my side or API side. – Sumit k Aug 31 '18 at 09:53
  • CORS needs to be fixed on the server side. You can't override it on the client - that's the whole point of it. The server decides what set of client conditions to accept requests from. – peeebeee Aug 31 '18 at 10:25
  • if it's not your API and the provider does not support CORS (or will not add you to their list of safe origins) then simply you cannot use AJAX to make this request. You'll have to make the request from your server-side code instead, and then pass the response back into your page. – ADyson Aug 31 '18 at 10:57

1 Answers1

0

You need to set up CORS to accept the origin from which the API is being called. I.e if the app that is calling this function is ABC.com the API must allow origin ABC.com.

Joe A
  • 13
  • 2
  • 4
  • it means i have to do something in API – Sumit k Aug 31 '18 at 09:36
  • @user3326557 yes you need to set the correct headers in your server's response. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS provides a good intro to the topic. – ADyson Aug 31 '18 at 09:46