0

jsonp http methods besides GET (POST, PUT, OPTIONS, DELETE)

Using jquery built-in $.ajax method looks like this

$(document).ready(function() {
    $.ajax({
    type: "GET",
    url: "http://myurl.com/webservice&callback=?",
    ...
});

Only want to draw attention to the line type: "GET", With $.ajax performing a http PUT would be simply change type: "PUT",

This code example comes from JSON parsing from cross domain using jquery ajax

Not using $.ajax

Using google-code's jquery.jsonp https://github.com/jaubourg/jquery-jsonp

Here is an example of using jquery.jsonp.js with the GET method

$.jsonp({
    cache: false,
    url: 'http://www.mydomain.com/logicalurl/2/',
    callbackParameter: 'callback',
    timeout: 10000,
    success: function(json, textStatus, xOptions) {
        myglob = json;
        MyModulePatternObject.initNew(json);
    },
    error: function (xOptions, textStatus) {
    console.log("fail");
    }
});

This works perfectly. How to do a GET jsonp request is not my question.

In $.jsonp, would like to perform the other http methods: PUT POST DELETE OPTIONS ... ? Does $.jsonp support the type="PUT",?

It's not mentioned at all in the docs: API.md and TipsAndTricks.md Nor in the source code.

UPDATE

@ohgodwhy There is a hack (iframes / Proxy) to get POST 2 work cross domains. Using PUT/POST/DELETE with JSONP and jQuery

@thefrontender Linked article suggests looking into, "Cross-Origin Resource Sharing (CORS)"

CORS support by browser http://caniuse.com/cors

Same article also says, "You could encode JSON as a URL parameter, but shame on you for even thinking that." In all of history, shame never stopped anyone? Simple, lazy, and in limited cases gets the job done.

Thx 4 everyones help ...

Community
  • 1
  • 1
faulkmore
  • 61
  • 2
  • 6

1 Answers1

2

JSON-P works by injecting a script tag into your document: it is not a traditional XHR request.

So you can usually only perform GET requests. You can NOT perform PUT requests.

More details in this write-up: http://johnnywey.wordpress.com/2012/05/20/jsonp-how-does-it-work/

thefrontender
  • 1,844
  • 14
  • 22
  • Actually, that's wrong. It's possible, albeit not the easiest. [Please see this detailed post here, it's amazing](http://stackoverflow.com/questions/5345493/using-put-post-delete-with-jsonp-and-jquery). – Ohgodwhy May 22 '13 at 05:41
  • 1
    @Ohgodwhy That's a very clever hack ... but it's a hack, is not built into the library being used and doesn't work for PUT (which the question specifically asked about) – thefrontender May 22 '13 at 05:48
  • I'd go as far as arguing that the method at the linked post doesn't use JSONP at all, which doesn't really help argue that jsonp can be done as anything other than GET (unless you consider a same-origin request using xhr that uses jsonp-style responses a true jsonp request). It's a clever trick, but it's not jsonp. It's just another way of circumventing the same-origin policy. This answer is correct. – Kevin B May 22 '13 at 05:57
  • @KevinB I'm inclined to agree with you, but there are those who think of JSON-P as a payload format rather than a communication method. And that takes us into slippery semantic slope territory that's not particularly helpful in the scope of this question. – thefrontender May 22 '13 at 06:05
  • @Ohgodwhy and this guys answers are both worth trying. http://stackoverflow.com/a/14145058/2255936 Don't mind clever tricks as long as they work and are supported by most browsers (read besides IE) – faulkmore May 22 '13 at 08:26
  • The conclusion of the post by @Ohgodwhy says, "CORS >= Proxy > JSONP (most of the time)." – faulkmore May 22 '13 at 08:37