做Asp.Net网站开发,少不了使用ajax技术,我平时也经常用ajax但总觉得了解的不透彻,今天在这里总结一下,也给有需要的人提供一点帮助。前台我不太理解的是dataType=text和dataType=json有啥区别,后台我想搞清楚能不序列化直接返回对象吗?下面将揭晓答案。
后台返回string
- public void ProcessRequest(HttpContext context)
- {
- try
- {
- string action = context.Request.Form["action"];
- string param1 = context.Request.Form["param1"];
- switch (action)
- {
- case "getString":
- //【1】转化成json返回
- context.Response.Write(JsonHelper.Serialize(GetString()));
- //【2】直接返回
- //context.Response.Write(GetString());
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- context.Response.Write("error");
- }
- }
-
- private string GetString()
- {
- return "abcde";
- }
- function GetStringText() {
- $.ajax({
- type: "post",url: "Handler1.ashx",contentType: "application/x-www-form-urlencoded;charset=utf-8;",data: { action: "getString",param1: "aaa" },dataType: "text",success: function(data) {
- //【1】后台数据转化成json字符串
- alert("data=" + data);
- },error: function(error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=text运行截图:
后台数据直接返回,dataType=text运行截图:
前台代码dataType=json:
后台数据进行json序列化,dataType=json运行截图:
后台数据直接返回,dataType=json运行截图:
后台返回string[]
- public void ProcessRequest(HttpContext context)
- {
- try
- {
- string action = context.Request.Form["action"];
- string param1 = context.Request.Form["param1"];
- switch (action)
- {
- case "getArray":
- //【1】转化成json返回
- context.Response.Write(JsonHelper.Serialize(GetArray()));
- //【2】直接返回
- //context.Response.Write(GetArray());
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- context.Response.Write("error");
- }
- }
-
- private string[] GetArray()
- {
- string[] array = {"aaa","bbb","ccc","ddd","eee"};
- return array;
- }
- function GetArrayText() {
- $.ajax({
- type: "post",data: { action: "getArray",success: function(data) {
- //【1】后台数据转化成json字符串
- var json = eval('(' + data + ')');
- alert("data=" + data + "\r\n" +
- "json[0]=" + json[0] + "\r\n" +
- "json[1]=" + json[1] + "\r\n" +
- "json[2]=" + json[2]);
-
- //【2】后台数据不进行转化data="System.string[]"
- //alert("data=" + data + "\r\n" +
- // "data[0]=" + data[0] + "\r\n" +
- // "data[1]=" + data[1] + "\r\n" +
- // "data[2]=" + data[2]);
- },error: function(error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=text运行截图:
后台数据直接返回,dataType=text运行截图:
前台代码dataType=json:
- function GetArrayJosn() {
- $.ajax({
- type: "post",success: function (data) {
- //【1】后台数据转化成json字符串
- alert("data=" + data + "\r\n" +
- "data[0]=" + data[0] + "\r\n" +
- "data[1]=" + data[1] + "\r\n" +
- "data[2]=" + data[2]);
-
- //【2】后台数据不进行转化直接进入error处理分支
-
- },error: function (error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=json运行截图:
后台数据直接返回,dataType=json运行截图:
后台返回Dictionary
- public void ProcessRequest(HttpContext context)
- {
- try
- {
- string action = context.Request.Form["action"];
- string param1 = context.Request.Form["param1"];
- switch (action)
- {
- case "getDictionary":
- //【1】转化成json返回
- context.Response.Write(JsonHelper.Serialize(GetDictionary()));
- //【2】直接返回
- //context.Response.Write(GetDictionary());
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- context.Response.Write("error");
- }
- }
-
- private Dictionary<string,object> GetDictionary()
- {
- Dictionary<string,object> dict = new Dictionary<string,object>()
- {
- {"name","guo"},{"age",18},{"address","唐宁街十号"}
- };
- return dict;
- }
- function GetDictionaryText() {
- $.ajax({
- type: "post",data: { action: "getDictionary",success: function(data) {
- //【1】后台数据转化成json字符串
- var json = eval('(' + data + ')');
- alert("data=" + data + "\r\n" +
- "json=" + json + "\r\n" +
- "json[name]=" + json["name"] + "\r\n" +
- "json[age]=" + json["age"] + "\r\n" +
- "json[address]=" + json["address"]);
-
- //【2】后台数据不进行转化
- //alert("data=" + data + "\r\n" +
- // "data[0]=" + data[0] + "\r\n" +
- // "data[1]=" + data[1] + "\r\n" +
- // "data[2]=" + data[2]);
- },error: function(error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=text运行截图:
后台数据直接返回,dataType=text运行截图:
前台代码dataType=json:
- function GetDictionaryJosn() {
- $.ajax({
- type: "post",success: function (data) {
- //【1】后台数据转化成json字符串
- alert("data=" + data + "\r\n" +
- "data[name]=" + data["name"] + "\r\n" +
- "data[age]=" + data["age"] + "\r\n" +
- "data[address]=" + data["address"]);
-
- //【2】后台数据不进行转化直接进入error处理分支
- },error: function (error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=json运行截图:
后台数据直接返回,dataType=json运行截图:
后台返回class
- public void ProcessRequest(HttpContext context)
- {
- try
- {
- string action = context.Request.Form["action"];
- string param1 = context.Request.Form["param1"];
- switch (action)
- {
- case "getClass":
- //【1】转化成json返回
- context.Response.Write(JsonHelper.Serialize(GetClass()));
- //【2】直接返回
- //context.Response.Write(GetClass());
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- context.Response.Write("error");
- }
- }
-
- private Student GetClass()
- {
- Student objStudent = new Student()
- {
- Name = "guo",Age = 18
- };
- return objStudent;
- }
- function GetClassText() {
- $.ajax({
- type: "post",data: { action: "getClass",success: function (data) {
- //【1】后台数据转化成json字符串
- var json = eval('(' + data + ')');
- alert("data=" + data + "\r\n" +
- "json=" + json + "\r\n" +
- "json[Name]=" + json["Name"] + "\r\n" +
- "json[Age]=" + json["Age"]);
-
- //【2】后台数据不进行转化
- //alert("data=" + data + "\r\n" +
- // "data[0]=" + data[0] + "\r\n" +
- // "data[1]=" + data[1] + "\r\n" +
- // "data[2]=" + data[2]);
- },error: function (error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=text运行截图:
后台数据直接返回,dataType=text运行截图:
前台代码dataType=json:
后台数据进行json序列化,dataType=json运行截图:
后台数据直接返回,dataType=json运行截图:
后台返回json字符串
- public void ProcessRequest(HttpContext context)
- {
- try
- {
- string action = context.Request.Form["action"];
- string param1 = context.Request.Form["param1"];
- switch (action)
- {
- case "getJson":
- //【1】转化成json返回
- context.Response.Write(JsonHelper.Serialize(GetJson()));
- //【2】直接返回
- //context.Response.Write(GetJson());
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- context.Response.Write("error");
- }
- }
-
- private string GetJson()
- {
- string json = "{\"name\":\"guo\",\"age\":\"18\",\"content\":{\"phone\":\"18233199999\",\"address\":\"唐宁街十号\"}}";
- return json;
- }
- function GetJsonText() {
- $.ajax({
- type: "post",data: { action: "getJson",success: function(data) {
- //【1】后台数据转化成json字符串
- var json = eval('(' + data + ')');
- json = eval('(' + json + ')');
- alert("data=" + data + "\r\n" +
- "json=" + json + "\r\n" +
- "json[name]=" + json["name"] + "\r\n" +
- "json[age]=" + json["age"] + "\r\n" +
- "json[content]=" + json["content"] + "\r\n" +
- "json[content][phone]=" + json["content"]["phone"] + "\r\n" +
- "json[content][address]=" + json["content"]["address"]);
-
- //【2】后台数据不进行转化
- //var json = eval('(' + data + ')');
- //alert("data=" + data + "\r\n" +
- // "json=" + json + "\r\n" +
- // "json[name]=" + json["name"] + "\r\n" +
- // "json[age]=" + json["age"] + "\r\n" +
- // "json[content]=" + json["content"] + "\r\n" +
- // "json[content][phone]=" + json["content"]["phone"] + "\r\n" +
- // "json[content][address]=" + json["content"]["address"]);
- },error: function(error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=text运行截图:
后台数据直接返回,dataType=text运行截图:
前台代码dataType=json:
- function GetJsonJosn() {
- $.ajax({
- type: "post",success: function (data) {
- //【1】后台数据转化成json字符串
- var json = eval('(' + data + ')');
- alert("data=" + data + "\r\n" +
- "json=" + json + "\r\n" +
- "json[name]=" + json["name"] + "\r\n" +
- "json[age]=" + json["age"] + "\r\n" +
- "json[content]=" + json["content"] + "\r\n" +
- "json[content][phone]=" + json["content"]["phone"] + "\r\n" +
- "json[content][address]=" + json["content"]["address"]);
-
- //【2】后台数据不进行转化
- //alert("data=" + data + "\r\n" +
- // "data[name]=" + data["name"] + "\r\n" +
- // "data[age]=" + data["age"] + "\r\n" +
- // "data[content]=" + data["content"] + "\r\n" +
- // "data[content][phone]=" + data["content"]["phone"] + "\r\n" +
- // "data[content][address]=" + data["content"]["address"]);
- },error: function (error) {
- alert("error=" + error);
- }
- });
- }
后台数据进行json序列化,dataType=json运行截图:
后台数据直接返回,dataType=json运行截图: