ajax與後台通訊,ajax後台通訊
前台代碼:
<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中的結果將是整個頁面。