“jquery.jsonp.js” GET works. What about POST PUT D

2020-03-31 06:42发布

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 ...

1条回答
趁早两清
2楼-- · 2020-03-31 07:07

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/

查看更多
登录 后发表回答