【发布时间】:2013-05-17 01:14:27
【问题描述】:
GET 以外的 jsonp http 方法(POST、PUT、OPTIONS、DELETE)
使用jquery内置的$.ajax方法是这样的
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://myurl.com/webservice&callback=?",
...
});
只想关注type: "GET",这一行
使用 $.ajax 执行 http PUT 只需更改 type: "PUT",
此代码示例来自JSON parsing from cross domain using jquery ajax
不使用 $.ajax
使用 google-code 的 jquery.jsonp https://github.com/jaubourg/jquery-jsonp
这里是一个使用 GET 方法的 jquery.jsonp.js 示例
$.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");
}
});
这非常有效。如何做一个 GET jsonp 请求不是我的问题。
在 $.jsonp 中,想要执行其他 http 方法:PUT POST DELETE OPTIONS ... ? $.jsonp 是否支持type="PUT",?
文档中根本没有提及:API.md 和 TipsAndTricks.md 源代码中也没有。
更新
@ohgodwhy 有一个 hack (iframes / Proxy) 可以让 POST 2 跨域工作。 Using PUT/POST/DELETE with JSONP and jQuery
@thefrontender 链接文章建议研究“跨源资源共享 (CORS)”
浏览器支持CORS http://caniuse.com/cors
同一篇文章还说,“您可以将 JSON 编码为 URL 参数,但您竟然这么想也感到羞耻。”在整个历史中,羞耻从未阻止过任何人?简单、懒惰且在有限的情况下可以完成工作。
感谢 4 位大家的帮助...
【问题讨论】:
-
"$.jsonp 是否支持 type="PUT",?" 没有。
-
谢谢大家。 json-p 已经不再使用了吗?
-
是的,JSONP 仍然被广泛使用。
标签: jquery jsonp google-code http-put http-method