locked
Access WebAPI in another domain using jquery RRS feed

  • Question

  • User986307670 posted

    I am trying to send a request to the Web API hosted in another domain using jquery AJAX Request. In fiddler when I intercept the calls it makes a OPTIONS request instead of a  GET request and there is no response either. However the same works with POSTMAN. How do I call another Web API or service from  jquery.

    Monday, February 1, 2016 5:28 AM

Answers

  • User1644485212 posted

    Hello Chitra,

    You are trying to make CORS request so it will make a "Preflight" request to the service with OPTIONS rather than GET or POST, and after getting 200 ok it will execute the actual request. So there has to be something at service level which returns 200 for all preflight request. Kindly go through below url,

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2016 10:22 AM

All replies

  • User603616845 posted

    Hi,

    Chitra Kanakavelu

    Access WebAPI in another domain using jquery

    Jquery is subject to the same domain policy. This means for security a JS Script in a client browser can only access the same domain as it came from.

    JSONP is not subject to the same restrictions. Check the jQuery docs on JSONP here:

    http://api.jquery.com/jQuery.getJSON/

    Here is a working example of using JSONP to access a cross-domain service via JQuery AJAX:

    http://jsbin.com/idasay/4

    And just in case JSBIN deletes this paste in the future:

    jQuery.ajax({
         type: "GET",
         url: 'http://api.geonames.org/postalCodeLookupJSON?postalcode=6600&country=AT&username=demo',
         dataType: "jsonp",
         cache: false,
         crossDomain: true,
         processData: true,
    
    
         success: function (data) {
             alert(JSON.stringify(data));
         },
         error: function (XMLHttpRequest, textStatus, errorThrown) {
             alert("error");
         }
     });

    You can go with this nice article to know how to get the data from cross domain.

    Hope this will help you.

    thanks

    </div>

    Monday, February 1, 2016 5:32 AM
  • User986307670 posted

    Thanks Mukesh for your reply. 

    However, this requires changes to the service to include additional headers or change the format of the body. However, we are creating a utility similar to Google postman and we do not have control over the services provided.  

    Is this achievable without making any changes to the service?

    Tuesday, February 2, 2016 5:22 AM
  • User1644485212 posted

    Hello Chitra,

    You are trying to make CORS request so it will make a "Preflight" request to the service with OPTIONS rather than GET or POST, and after getting 200 ok it will execute the actual request. So there has to be something at service level which returns 200 for all preflight request. Kindly go through below url,

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2016 10:22 AM