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中的結果將是整個頁面。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章