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

About saxenapraveen

I work with Microsoft and have 12 years of experience in developing,designing and leading reliable and scalable solutions for cloud and device ready businesses.

Posted on September 7, 2012, in ASP.NET MVC, Asp.Net WebApi, Asp.Net WebForms, Http services, Jquery, Jquery and tagged , . Bookmark the permalink. 1 Comment.

  1. I’m no longer certain the place you are getting your information, however good topic. I needs to spend some time learning more or figuring out more. Thank you for fantastic information I was looking for this information for my mission.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: