Jquery AJAX的一些示例

Ajax GET示例1
请求页面返回:{"errcode":40018,"errmsg":"invalid button name size"}

function Public_Menu() {
$.ajax({
type: "GET",
url: "Ajax_FinalPublicMenu.aspx",
dataType: "json",
success: function (data) {
var retcode = data.errmsg;
if (retcode = "ok") {
alert("发布成功!");
}
else {
alert("发布失败!错误原因:" + retcode);
}

},
error: function () {
alert("发布失败!错误原因:请求失败。")
}

});


}

Ajax GET示例2:
请求页面返回:{"errcode":40018,"errmsg":"invalid button name size"}
{
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}

获取返回数据:data.programmers[0].firstName

Ajax POST示例1:
返回形式已定义为json,循环输出json中数据

$.ajax({
type: "POST",
dataType: "json",
url: "Ajax_MsgList.aspx",
data: { typeid: Msgindex },
success: function (data) {
//alert(data);
selectzone.empty();
$.each(data, function (i, n) {
$("<option value=" + n.id + ">" + n.option + "</option>").appendTo(selectzone);
});
//如果当前菜单选择过,则变为已选择过的选项
if (Existindex != 0) {
$("#Select_MsgContent_Main").val(Existindex);
}
}
});

Ajax POST示例2:
返回形势没有定义为json,但是要解析成json

$.post("Ajax_ExistMsg_SubMenu.aspx",
{
postid: MainID * 10 + SubID,type:"sub" //postid:菜单id type:菜单类型(main或sub)
},
function (data, status) {
//将返回结果格式化为json数据
var jsonx = eval("(" + data + ")");
var json_msgtype = jsonx.msg[0].msgtype;
var json_msgid = jsonx.msg[0].msgid;
if (json_msgtype != 0 && json_msgid != 0) {
$("#select_MsgType").val(json_msgtype);
ShowMsgContent(json_msgtype, json_msgid);

}
else {
$("#select_MsgType").val(0);
ShowMsgContent(0, 0);
}

// document.getElementById("xxd").value=data;


});

Jquery AJAX的一些示例,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。