ajax用法

Posted fiting

tags:

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

$.ajax({
url: "../AjaxPage/AjaxHandler.ashx",
cache: false,
//type:"Post",
data: { Type: "Getreport", gettexvalue: gettexvalue, tablename: tablename },
dataType: "json",
success: function (data) {
alert("保存成功");
}
});

 

返回jaso 一定要下载jason相关版本

 

<%@ WebHandler Language="C#" Class="AjaxHandler" %>

using System;
using System.Web;
using Newtonsoft.Json;
using System.Data;
using System.Data.SqlClient;

public class AjaxHandler : IHttpHandler
{

string sql = "";
SqlDataAccess sda = new SqlDataAccess();
SqlParameterCollection spc = new SqlCommand().Parameters;
MethodKit mk = new MethodKit();
DataSet ds = new DataSet();
public void ProcessRequest (HttpContext context) {
try
{
context.Response.ContentType = "text/plain";
string type = context.Request["Type"].ToString();
string date = System.DateTime.Now.ToString("yyyy-MM-dd");
string rtn = null;
string gettexvalue = "";
string tablename = "";
switch (type)
{
case "Getreport"://各个报表下面或者上面的备注插入不同的数据表中
gettexvalue = context.Request["gettexvalue"];
tablename = context.Request["tablename"];
rtn = bindreport(gettexvalue, tablename, date);
break;
}
context.Response.Write(rtn);
}
catch (Exception exp)
{

}
}

private string bindreport(string gettexvalue, string tablename, string date)
{
string upsql = "";
string sql1 = "select * from " + tablename + " where CONVERT(varchar(100), Date, 23)=‘" + date + "‘";
System.Data.DataTable sb2 = sda.ExecSqlQuery(sql1.ToString()).Tables[0];
//如果表里存在今日数据就更新此表
if (sb2.Rows.Count > 0)
{
upsql = "update ‘" + tablename + "‘ set TextName=‘" + gettexvalue + "‘ where Date=‘" + date + "‘";
}
else {
upsql = "Insert into " + tablename + " values(‘" + gettexvalue + "‘,‘" + date + "‘)";
}

sda.ExecSqlQuery(upsql.ToString());

string sql2 = "select * from " + tablename + " where CONVERT(varchar(100), Date, 23)=‘" + date + "‘";
System.Data.DataTable sb3 = sda.ExecSqlQuery(sql2.ToString()).Tables[0];
return Newtonsoft.Json.JsonConvert.SerializeObject(sb3);
}
public bool IsReusable
{
get
{
return false;
}
}

}

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

ajax定义和用法

AJAX 的简单用法:

原生的ajax和jQuery Ajax的用法

jqPaginator分页(ajax用法和form表单提交用法)

最简单ajax,$.post()用法

Ajax简单用法