jquery的postJSON

来源:互联网 发布:oracle数据库管理规范 编辑:程序博客网 时间:2024/05/22 13:12

转自:http://www.cnblogs.com/lusionx/archive/2009/03/08/1777061.html

http://fanxiaojie.com/article.asp?id=59

postJSON


jQuery.postJSON = function(data, url, success) {
return jQuery.ajax({
type: "POST",
url: url,
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: success
});
};

使用

$(function() {
$.postJSON("{}", "Validate.asmx/GetPs", funOK);
})

function funOK(msg) {
$.each(msg.d, function() {
for (ee in this) {
if (ee != '__type') {
$('#inf').append(this[ee]);
}
}
})

}

第二部分

以前我看到jquery API.chm里写着$.ajax()有三个扩展,分别是$.get(),$.getJSON,$.post()。 我感到很奇怪:为什么没有$.postJSON()方法呢?如果我要用post方法传递数据,并取加回json型数据怎么办?我发现那个.chm手册里写着:$.get(),$.getJSON,$.post()都有三个参数,分别是url,[data],[callback]。可是我恰好刚才需要用到用post方法传递数据,并取加回json型数据这种方法。怎么办?难道用那繁复的$.ajax函数,把参数一个一个地写出来?我想我会头痛的。呵呵。后来我打开了那个jquery.1.3.2-min.js看了看,发现原来$.get和$.post都是有四个参数的,而不是三个参数。第四个参数也是可选的,如果不写的话,就取默认值text。原来如此!

于是我就知道:我可以用$.post(url,[data],[callback],'json')这个写法来做到用post方法传递数据,并取加回json型数据。如果我要取回的数据类型是xml的,就可以写成$.post(url,[data],[callback],'xml')。对$.get()方法也一样。另外呢,我发现其实$.get(url,[data],[callback],'json')和$.postJSON(url,[data],[callback])是等效的。

原创粉丝点击