首页 > 代码库 > ajax与后台通信 -- Response.End()

ajax与后台通信 -- Response.End()

前台代码:

<span style="font-size:14px;">var json = {
    width: w,
    height: h,
    category: canvas_category,
    name: canvas_json_name,
    description: canvas_description,
    border: canvas_border,
    lineWidth: defaultLineW,
    json: canvas_json
};</span>

 

<span style="font-size:14px;">$.ajax({ url: "Canvas_panel.aspx", data: json,
    success: function (result)
    {
        if (result == "Exist")
        {
            alert("There is a same record in DB, you can't save it.");
        }
        else if (result == "Success")
        {
            alert('Save Success.');
        }
    },
    error: function (err)
    {
        alert(err);
    }
});</span>

参数可以写成json格式,放到data中传输,也可以加到url中用queryString方式传输。

后台代码:

<span style="font-size:14px;">if (Request["name"] != null)
{
	int width = int.Parse(Request["width"].ToString());
	int height = int.Parse(Request["height"].ToString());
	string name = Request["name"].ToString();
	string json = Request["json"].ToString();

	string sql = "select * from warehouse_model where code='" + name + "' and json='" + strJson + "'";
	DataTable dtValidate = _dataAccess.GetTables(sql);
	if (dtValidate.Rows.Count > 0)
	{
		Response.Write("Exist");
		Response.End();
	}
	else
	{
		Response.Write("Success");
		Response.End();
	}
}</span>

注意这里面的Reponse.End()方法,它的含义是强迫Web服务器停止执行更多的脚本,并发送当前结果,文件中剩余的内容将不被处理。如果不加上这个方法,前台Result中的结果将是整个页面。

ajax与后台通信 -- Response.End()