为了更好的说明post 的用法,结合C#的ashx程序
ashx的程序代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace web
{
/// <summary>
/// ajax 的摘要说明
/// </summary>
public class ajax : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (context.Request["fun"] != "load")
su(context);
else
{
load(context);
}
}
public static void su(HttpContext context)
{
if (context.Request["name2"] == null || context.Request["area2"] == null)
{
context.Response.Write("不能为空");
}
else
{ SqlConnection cn = new SqlConnection("server=.\\SQLEXPRESS;database=asd;uid=zy;pwd=123456;");
cn.Open();
string name2 = null;
name2 = context.Request["name2"];
string area2 = null;
area2 = context.Request["area2"];
string sql = "insert into yan values('" + name2 + "','" + area2 + "','" + DateTime.Now.ToString() + "','"+null+"')";
SqlCommand cmd = new SqlCommand(sql, cn);
cmd.ExecuteNonQuery();
cn.Close();
context.Response.Write("提交成功");
}
}
public static void load(HttpContext context)
{
SqlConnection cn = new SqlConnection("server=.\\SQLEXPRESS;database=asd;uid=zy;pwd=123456;");
cn.Open();
string sql = "select top 1 name,content1,time from yan order by time desc";
SqlDataReader dr ;
SqlCommand cmd = new SqlCommand(sql, cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{
context.Response.Write(dr[0].ToString()+"@"+dr[1].ToString()+"@"+dr[2].ToString());
}
//context.Response.Write(sda);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
html的js事件代码
$(document).ready(function () {
$.post("../ajax.ashx",
{ fun:"load"
},
function(data,status)
{
//alert("数据:" + data);
var dataobj = data;
var d = dataobj.split("@");//转化为json格式,易于使用
document.getElementById("name61").innerHTML = d[0];
document.getElementById("content").innerHTML = d[1];
document.getElementById("time").innerHTML = d[2];
});
});
探讨
1、Get是用来从服务器上获得数据,而Post是用来向服务器上传递数据。
2、Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接;Post是将表单中的数据放在form的数据体中,按照变量和值相对应的方式,传递到action所指向URL。3、Get是不安全的,因为在传输过程,数据被放在请求的URL中,而如今现有的很多服务器、代理服务器或者用户代理都会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前。Post的所有操作对用户来说都是不可见的。
4、Get传输的数据量小,这主要是因为受URL长度限制;而Post可以传输大量的数据,所以在上传文件只能使用Post(当然还有一个原因,将在后面的提到)。
5、Get限制Form表单的数据集的值必须为ASCII字符;而Post支持整个ISO10646字符集。默认是用ISO-8859-1编码
6、Get是Form的默认方法。