Hii我在ajax中完成了以下方法,我必须插入星期一,星期二,星期三像天,但它按非顺序插入有时它会先插入星期一,有时它会先插入星期四请帮助我。我提到很多网站,所有告诉实施睡眠是否有任何其他方式做事先谢谢
for (i = 1; i <= x; i++) {
AvailDay = $.trim($('#day_' + i).text()).replace(/[\s\n\r]+/g, ' ');
alert(AvailDay);
AMFrom = $('#amfrom_' + i).val();
AMTo = $('#amto_' + i).val();
PMFrom = $('#pmfrom_' + i).val();
PMTo = $('#pmto_' + i).val();
$.ajax({
type: "POST",
url: "ProviderSignup.aspx/AddAvailibility",
data: "{'ProviderId':'" + parseInt(ProviderId) + "','AvailDay':'" + AvailDay + "','AMFrom':'" + AMFrom + "','AMTo':'" + AMTo + "','PMFrom':'" + PMFrom + "','PMTo':'" + PMTo + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function () {
alert("Error");
}
});
}
我的C#代码
[WebMethod]
public static Int32 AddAvailibility(Int32 ProviderId,string AvailDay,string AMFrom, string AMTo, string PMFrom,string PMTo)
{
string strconn = (string)ConfigurationSettings.AppSettings["ConnectionStringLocal"];
List<Speaclity> Detail = new List<Speaclity>();
string Success = string.Empty;
string SelectString = "Insert Into T_Provider(ProviderId, AvailDay, AMFrom, AMTo, PMFrom, PMTo) values ('" + ProviderId + "','" + AvailDay + "','" + AMFrom + "','" + AMTo + "','" + PMFrom + "','" + PMTo + "') ";
SqlConnection cn = new SqlConnection(strconn);
cn.Open();
SqlCommand cmd = new SqlCommand(SelectString, cn);
int result = cmd.ExecuteNonQuery();
if (result != 0)
{
Success = "True";
}
else
{
Success = "False";
}
return result;
}
答案 0 :(得分:0)
$ .ajax(...)默认为异步。请尝试 async:“false”
$.ajax({
async: false,
type: "POST",
url: "ProviderSignup.aspx/AddAvailibility",
data: "{'ProviderId':'" + parseInt(ProviderId) + "','AvailDay':'" + AvailDay + "','AMFrom':'" + AMFrom + "','AMTo':'" + AMTo + "','PMFrom':'" + PMFrom + "','PMTo':'" + PMTo + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
},
error: function () {
alert("Error");
}
});
在这种情况下,js函数将在发送下一个请求之前等待结果。