用ajax传JSON数据

Posted sunshinezjb

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用ajax传JSON数据相关的知识,希望对你有一定的参考价值。

/// <reference path="jquery-1.10.2.js" />
$(function () {
$("#btnLogin").click(function () {
var qq = $.trim($("#txtQQ").val());
var pwd = $.trim($("#txtPwd").val());
if (qq == "" || pwd == "") {
alert("qq或密码不能为空");
} else {
$.post("../ajax/Handler1.ashx", { "qq": qq, "pwd": pwd }, function (data) {
//switch (data) {
// case "1":
// alert("登录成功");
// break;
// case "2":
// alert("QQ或者密码错误");
// break;
// case "3":
// alert("程序异常");
// break;
// }

var data=JSON.parse(data);
if(data.Success==true){
alert(data.Msg);
}else{
alert(data.Msg);
}

});
};
})
})

 

 

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="exercise_20180305.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/WebForm1.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>QQ:</td><td><input type="text" id="txtQQ" /></td></tr>
<tr><td>密码:</td><td><input type="password" id="txtPwd" /></td></tr>
<tr><td><input type="button" value="登录" id="btnLogin" /></td></tr>
</table>
</div>
</form>
</body>
</html>

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections;

namespace exercise_20180305.ajax
{
public class ReturnMsg
{
public bool Success { get; set; }
public string Msg { get; set; }
public int ReCount { get; set; }
}
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
string connStr = ConfigurationManager.ConnectionStrings["sq_ruanmou"].ToString();
SqlConnection con = null;
SqlCommand cmd = null;

public void OpenDB()
{
con = new SqlConnection(connStr);
con.Open();
}
public void ProcessRequest(HttpContext context)
{
ReturnMsg rmsg = new ReturnMsg();
javascriptSerializer jss = new JavaScriptSerializer();
// Dictionary<string,object> dic=new Dictionary<string,object>;
try
{
string qq = context.Request.Form["qq"];
string pwd = context.Request.Form["pwd"];

string sql = "select count(*) from UserInfor where [email protected] and [email protected]";
SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@QQ",qq),
new SqlParameter("@Pwd",pwd)
};
OpenDB();
using (cmd = new SqlCommand(sql, con))
{

foreach (var p in para)
{
cmd.Parameters.Add(p);
};
int icount = Convert.ToInt32(cmd.ExecuteScalar().ToString());
rmsg.ReCount = icount;
if (icount > 0)
{
rmsg.Success = true;
rmsg.Msg = "登录成功";
//dic.Add("Success":true);
//dic.Add("Msg":"登录成功");
}
else
{
rmsg.Success = false;
rmsg.Msg = "QQ或者密码错误";
//dic.Add("Success":false);
//dic.Add("Msg":"QQ或者密码错误");
}
}
}
catch (Exception ex)
{
rmsg.Success = false;
rmsg.Msg = "程序异常";
//dic.Add("Success":false);
//dic.Add("Msg":"程序异常");
}
context.Response.Write(jss.Serialize(rmsg));
//context.Response.Write(jss.Serialize(dic));


}

public bool IsReusable
{
get
{
return false;
}
}
}
}



































































































































以上是关于用ajax传JSON数据的主要内容,如果未能解决你的问题,请参考以下文章

javascript用ajax向PHP传json格式数据,在PHP文件中应该怎样接?

PHP 怎么解析远程URL 传过来json ,在前端显示出来

ajax 使用POST方法传的数据服务端怎么接

ajax往后台传json格式数据报415错误

JSON字符串转换JSON对象,在JS页面转换成功,但是ajax提交json数据,后台解析抛出异常

后台怎么接受并解析前端ajax传过来的json