Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery ajax JSONP POST changed to GET when cross domain

Tags:

jquery

jsonp

wcf

I have a WCF Service that is expecting a POST. Using Fiddler I discovered that in cross-domain situations, my POST request was getting changed to a GET which results in error 405 from server.

$.ajax({
    type: "POST",
    url: "http://blah/blah.svc/Test",
    data: JSON.stringify("{ 'WebUserID': 4 }"),
    dataType: "jsonp",  // from server
    contentType: "application/json; charset=utf-8", // to server
    success: function (data, status, xhr) {
        alert("success--");
    }
});

Can anyone shed some light on this?

Thanks

like image 382
nuander Avatar asked May 09 '12 17:05

nuander


1 Answers

There's no POST and JSONP. JSONP works by creating a new script tag in the DOM which sends a GET request to the server. You're giving jQuery.ajax two incompatible parameters (POST, jsonp), and jQuery is choosing one over the other.

One update: you can use something like CORS (Cross-Origin Resource Sharing) to enable non-GET requests to cross-domain services. WCF doesn't support it out-of-the-box, but I wrote a post about implementing it in WCF at http://blogs.msdn.com/b/carlosfigueira/archive/2012/05/15/implementing-cors-support-in-wcf.aspx.

like image 181
carlosfigueira Avatar answered Oct 02 '22 09:10

carlosfigueira