十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
小編給大家分享一下ASP.NET如何用Ajax返回Json對象,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
十載的瀘縣網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整瀘縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“瀘縣網(wǎng)站設(shè)計”,“瀘縣網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
一、新建一個html頁面,如注冊頁面"Register.htm"
用戶注冊 用戶名:
密 碼:
姓 名:
二、新建一js文件,如:reg.js
$(function() {
//定義清除錯誤信息的函數(shù)
function clearMsg() {
$(".msg").html("");
}
//定義獲取表單數(shù)據(jù)的函數(shù),注意返回json對象
function formData() {
return {
id: $("#id").val(),
pwd: $("#pwd").val(),
name: $("#xm").val()
};
}
//定義注冊功能的函數(shù)
function reg() {
var url = "Register.ashx";
var data = formData();
clearMsg();
$.ajax({
type: 'GET', //自動會把json對象轉(zhuǎn)換為查詢字符串附在url后面如:http://localhost:49521/Register.ashx?id=a&pwd=b&name=c
url: url,
dataType: 'json', //要求三、處理ajax請求
方法一:手動拼接json字符串
新建一般處理程序,如:Register.ashx
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
namespace WebLogin
{
///
/// $codebehindclassname$ 的摘要說明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Register1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";//設(shè)置響應(yīng)內(nèi)容的格式是json格式
string id = context.Request["id"];
string pwd = context.Request["pwd"];
string name = context.Request["name"];
List msgList = new List();
if (String.IsNullOrEmpty(id))
{
msgList.Add("{\"id\":\"idMsg\",\"message\":\"用戶名不能為空.\"}");
}
if (pwd==null || pwd=="")
{
msgList.Add("{\"id\":\"pwdMsg\",\"message\":\"密碼不能為空.\"}");//形如:{"id":"pwdMsg","message":"密碼不能為空."}
}
if (name==null || name=="")
{
msgList.Add("{\"id\":\"nameMsg\",\"message\":\"姓名不能為空.\"}");
}
string responseText = "";
if (msgList.Count == 0)
{
//調(diào)用后臺代碼寫入數(shù)據(jù)庫
responseText = "{\"success\":true,\"message\":\"注冊成功\"}";
}
else
{
string msgsValue = "";
for (int i = 0; i < msgList.Count; i++)
{
msgsValue += msgList[i] + ",";//將列表中的每一個字符串連接起來,用","隔開,不過最后還會多","
}
msgsValue=msgsValue.Substring(0, msgsValue.Length - 1);//去掉末尾的","
msgsValue = "[" + msgsValue + "]";//用"[]"括起來,如:[{"id":"pwdMsg","message":"密碼不能為空."},{"id":"nameMsg","message":"姓名不能為空."}]
responseText = "{\"success\":false,\"message\":\"注冊失敗\",\"msgs\":" + msgsValue + "}";
//最的形如:{"success":false,"message":"注冊失敗","msgs":[{"id":"pwdMsg","message":"密碼不能為空."},{"id":"nameMsg","message":"姓名不能為空."}]}
}
context.Response.Write(responseText);
}
public bool IsReusable
{
get
{
return false;
}
}
}
} 方法二:使用Json.NET工具來將C#對象轉(zhuǎn)換json輸出
1、新建信息類“Msg.cs”
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace WebLogin
{
public class Msg
{
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
public Msg(string id, string message)
{
this.id = id;
this.message = message;
}
}
}2、新建返回json對象的類“ResponseData.cs”
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
namespace WebLogin
{
public class ResponseData
{
private bool success;
public bool Success
{
get { return success; }
set { success = value; }
}
private string message;
public string Message
{
get { return message; }
set { message = value; }
}
private List msgs;
public List Msgs
{
get { return msgs; }
set { msgs = value; }
}
public ResponseData(bool success, string message)
{
this.success = success;
this.message = message;
}
public ResponseData(bool success, string message, List msgs)
{
this.success = success;
this.message = message;
this.msgs = msgs;
}
}
} 3、去官網(wǎng)下載Json.NET,并復(fù)制引用
官網(wǎng):http://www.newtonsoft.com/json
下載地址:http://pan.baidu.com/s/1nvz9JBV
下載解壓后將“Newtonsoft.Json.dll”復(fù)制到項目的“bin”目錄中,并引用(注意和.net版本保持一致)
4、新建一般處理程序“reg.ashx”
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Collections.Generic;
using Newtonsoft.Json;//引入
namespace WebLogin
{
///
/// $codebehindclassname$ 的摘要說明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class reg : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";//設(shè)置響應(yīng)內(nèi)容的格式是json格式
string id = context.Request["id"];
string pwd = context.Request["pwd"];
string name = context.Request["name"];
List msgs = new List();
if (String.IsNullOrEmpty(id))
{
msgs.Add(new Msg("idMsg", "用戶名不能為空."));
}
if (String.IsNullOrEmpty(pwd))
{
msgs.Add(new Msg("pwdMsg", "密碼不能為空."));
}
if (String.IsNullOrEmpty(name))
{
msgs.Add(new Msg("nameMsg", "姓名不能為空."));
}
ResponseData rData;
if (msgs.Count == 0)
{
//調(diào)用注冊方法,寫入數(shù)據(jù)庫
rData = new ResponseData(true, "注冊成功.");
}
else
{
rData = new ResponseData(false, "注冊失敗.", msgs);
}
context.Response.Write(JsonConvert.SerializeObject(rData));//直接調(diào)用方法將rData轉(zhuǎn)換為json字符串
}
public bool IsReusable
{
get
{
return false;
}
}
}
} 四、完成效果如圖

以上是“ASP.NET如何用Ajax返回Json對象”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前文章:ASP.NET如何用Ajax返回Json對象
網(wǎng)頁路徑:http://m.jiaotiyi.com/article/psgsjg.html