5

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

nuander
  • 1,319
  • 1
  • 19
  • 33
  • 1
    This isn't really a duplicate, but the answer answers this question: http://stackoverflow.com/questions/2699277/post-data-to-jsonp – lonesomeday May 09 '12 at 17:32

2 Answers2

10

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.

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
0

It's converting it to GET because you no longer have a name/value pair after doing the JSON.stringify; you just have a string. POST requires a name/value pair.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • Don't `JSON.stringify` your data unless you're meaning to pass a serial representation of an object. – Jonathan M May 09 '12 at 17:39
  • He shouldn't stringify, but not because of what you mentioned. The parameter to `stringify` is already "stringified", it's not a JS object (it's a string). Stringifying will double-encode it, which will fail. – carlosfigueira May 09 '12 at 18:34
  • And to send the data in a POST request, he actually needs to pass a serialized version of the object (in the request body). – carlosfigueira May 09 '12 at 18:35
  • Yes. I think what he actually meant to do was: `data: { WebUserId: 4 },`. That's what I was trying to say. – Jonathan M May 09 '12 at 18:39
  • This was a simplification of what I'm really doing. I do mean to send a serialized object. However, I can find no version that works. Can someone suggest an example? – nuander May 09 '12 at 19:04
  • You'll then have to say `data: {myObject: JSON.stringify(myJSObject)},`. Then look for `myObject` on the server side. You'll have to parse it as JSON on the server. – Jonathan M May 09 '12 at 19:07
  • 1
    Thanks for the input but I think carlosfigueira is right. I don't think it can be done this way. – nuander May 09 '12 at 19:11
  • I've done it. What's not working? You mean passing a stringified object, or the POST? – Jonathan M May 09 '12 at 19:12
  • If the POST, then yes, now that I think about it, all jsonp can do is a get because it's just adding a ` – Jonathan M May 09 '12 at 19:18