同步提交&异步提交

1.在数据库提交点赞和踩的功能的时候

可以采取两种方法

a 同步提交

httprequest


b   异步提交

xmlhttprequest


Json  格式 :

javascript 的一个子集,是轻量级的数据交换格式

{}括号起来的一个集合,是一个集合对象

 

而{属性名:属性 }的格式

通常正确的格式是
{id=1,name='张三',age:18 }

join的数组表示形式

["java","javascript","vb"]

或:

[{id=1,name='zhangsan',age:18},....]

//在jquer中 $.ajax({


url:‘/testajax.ashx';
type:'post'
data:{"zan","zan"};
dataTyPe:"text";


});//利用json格式


实例:

点赞功能 异步提交:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-3.1.1.js"></script>
</head>
<body>
<video src="3.mp4" autoplay="autoplay" controls="controls"></video>
<p>
<input type="button" value="赞" id="zancunt"/><span id="zan"></span>
<input type="button" value="踩" id="caicunt" /><span id="cai"></span>
</p>
<script type="text/javascript">
$("#zancunt").click(function () {

 

$.ajax({

url: '/Handler1.ashx',
type: 'post',
data: { 'zancunt': "zancunt" },
datatype: 'text',
success: function (result) {

 

$("#zan").text(result);


}


})


})
</script>
</body>
</html>

 

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

namespace WebApplication19
{
public class SqlHelper
{
private static readonly string connString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
public static int ExecuteNonQuery(string sql,params SqlParameter[] parameters )
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteNonQuery();
}
}
}
public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddRange(parameters);
return cmd.ExecuteScalar();
}
}
}
public static DataTable ExecuteDataTable(string sql, params SqlParameter[] parameters)
{
DataSet set = new DataSet();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
if (parameters!=null)
{
cmd.Parameters.AddRange(parameters);
}
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(set);
return set.Tables[0];
}
}
}
}
}

<?xml version="1.0" encoding="utf-8"?>

<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<connectionStrings>
<add name="connStr" connectionString="server=.;database=video;uid=sa;pwd=123"/>
</connectionStrings>

</configuration>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace WebApplication19
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string zan = context.Request["zancunt"];
string cai = context.Request["caicunt"];
if (!string.IsNullOrEmpty(zan))
{


SqlHelper.ExecuteNonQuery("update [video].[dbo].[Table_1] set zan=zan+1");
int zancunt=Convert.ToInt32(SqlHelper.ExecuteScalar("select * from [video].[dbo].[Table_1]"));
context.Response.Write(zancunt);





}
}

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




posted @ 2016-12-05 21:34  西瓜冰镇老板  阅读(366)  评论(0编辑  收藏  举报