Blog Archives

Jquery – Cross domain script call – No transport error

I have spent few hours figuring out that why my below code is not working. This piece of code is beeing called from a asp.net website. It is calling a WebApi http service using jquery ajax api.

$(document).ready(function () {
$.ajax({
    url: "http://localhost:65209/api/values/SayHello",
    type: "GET",
    success: function (result) { alert(result);  },
    error:   function () { alert('error'); }
});

The reason was that using ajax I was trying to make a cross domain script call whiich by default will throw an error saying “No Transport”.
the solution is to introduce one line which was introduced after jquery 1.4.1 version onwards. Just add this line after $(document).ready() itself.

jQuery.support.cors = true;

That’s it! the same code works fine for me.

Note: Even though it works fine for you if you create your own service layer and website is trying to consume it. But there are few public websites  like netflix which does not allow cross domain scripting.

I hope it helps!

Advertisements