//前臺代碼
//#contact-form 表單ID
$(function () {
$("#contact-form").submit(function () {
$.ajax({
type: "post",
url: "?",
dataType: "text",
async: false,
data: $('#contact-form').serialize(),
success: function (data) {
debugger;
alert(data);
}
});
});
});
//後臺代碼
string name = Request.Form["name"];
string textEmail = Request.Form["email"];
string pone = Request.Form["pone"];
string messages = Request.Form["message"];
string Msg = "";
BusinessConsulted bcb = new BusinessConsulted
{
MessageName = name,
MessageEmail = textEmail,
ContactInformation = pone,
Messages = messages,
MessageTime = DateTime.Now.ToString(),
State = 0,
ClassifyOne = 0,
ClassifyTwo = 0
};
BusinessConsultedBLL bb = new BusinessConsultedBLL();
bb.InsertBusinessConsulted(bcb, ref Msg);
Response.Write(Msg);
Response.End();
調試的時候,進入後臺,如果是ajax異步,就是每行走兩次,同步就是走完一遍,在走一遍
前臺輸出的時候,也是輸出兩次,先輸出ajax回調的參數值,然後又顯示了正常表單提交的返回值
$("#contact-form").submit(function () {
$.ajax({
type: "post",
url: "?",
dataType: "text",
async: false,
data: $('#contact-form').serialize(),
success: function (data) {
debugger;
alert(data);
}
});
return false;
});