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:
请求页面返回:
{
"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 Get示例3:
请求页面返回:{"total":2,"count":2,"data":{"openid":["oqNsSt_RNvAIrmGXARi942wwnwKg","oqNsSt9fWSqEAvUV5SPnQbQdb33g"]},"next_openid":"oqNsSt9fWSqEAvUV5SPnQbQdb33g"}
获取解析返回数据:
var a = data.total;
var b = data.count;
var c = data.data["openid"][0];
document.getElementById("FansCount").innerHTML = a + "," + b + "," + c;

 


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;


});

 

posted @ 2014-03-17 09:50  copperWang  阅读(209)  评论(0编辑  收藏  举报