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": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"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的一些示例,布布扣,bubuko.com

Jquery AJAX的一些示例

上一篇:《Codeforces Round #739 (Div. 3)》


下一篇:如何突破PHP程序员的技术瓶颈分析