Ajax实现异步操作实例_针对JSON格式的请求数据

最近写了一篇ajax异步操作XML格式的,今天就写关于json格式的。

一、简单了解Json

1. JSON有两种表示结构,对象和数组。

1.1 对象:

{
    key1:value1,
    key2:value2,
    ...
}

1.2 数组

[
    {
        key1:value1,
        key2:value2 
    },
    {
         key3:value3,
         key4:value4   
    }
]

二、ajax如何利用json对象数据传递

$.ajax({
	type: "post",
	url: "Hand/AjaxJson.ashx",
	data: {"AreaId":"123"},
	datatype:"json",
	success: function(jsondata){
		alert(jsondata.id);
	},
	error:function(XMLResponse){alert(XMLResponse.responseText)}
});

  

 三、后台如何接受和处理传递json对象参数

            context.Response.ContentType = "text/json";
            //context.Response.Write("Hello World");
            //获取post传入的值
            string strxml = context.Request["AreaId"].ToString();
            StringBuilder json = new StringBuilder();
            json.Append("{");
            json.Append(""id": "5"");
            json.Append(","name": "ma"");
            json.Append("}");
            string returndata = json.ToString();
            context.Response.Write(returndata);    

  

原文地址:https://www.cnblogs.com/WarBlog/p/4648593.html