gpt4 book ai didi

c# - AJAX 请求错误

转载 作者:行者123 更新时间:2023-11-30 17:40:51 28 4
gpt4 key购买 nike

我在提交 ajax 请求时遇到错误。

这是 jQuery/ajax

  <script type="text/javascript">
$(function () {
$('#BookButton').click(function (event) {
var form = $('#Form1');
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: $("#BookRoom :input").serialize()
}).done(function (data) {
$('#BookRoom').modal('hide');
if (data === ResponseType.Success) {
$('#SuccessMsg').text('Meeting Booked');
$('#SuccessMessage').modal('show');
}
else if (data === ResponseType.Reject) {
$('#SuccessMsg').text('There are conflicts');
$('#SuccessMessage').modal('show');
}
else if (data === ResponseType.Reject) {
// Notify user: It is your fault
}
else {
$('#SuccessMsg').text('Test');
$('#SuccessMessage').modal('show');
}


// Optionally alert the user of success here...


setTimeout(function () { $('#SuccessMessage').modal('hide'); }, 3000);
}).fail(function () {
// Optionally alert the user of an error here...
alert("Error submitting AJAX request");
});
event.preventDefault(); // Prevent the form from submitting via the browser.
});
});

C#总结

            public enum ReponseType : int
{
Success = 0,
Reject = 1

}



if (ITMtgfapts.Items.Count > 0) // If there is more than one item
{


Response.AddHeader("Content-type", "application/json");
Response.ContentType = "application/json";
Response.Write(JsonConvert.SerializeObject(ReponseType.Reject));
Response.End();
}


else
{

//Success
appointment.RequiredAttendees.Add(roomEmail);



appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
Response.AddHeader("Content-type", "application/json");
Response.ContentType = "application/json";
Response.Write(JsonConvert.SerializeObject(ReponseType.Success));
Response.End();


}

值得注意的是,这是一个 WebForms 应用程序。当我提交表单时,我得到:

  alert("Error submitting AJAX request");

错误信息。然而 C# 仍然执行(即它预订 session 室)

有什么想法吗?

谢谢

编辑:

fiddler 响应

JSON:

ResponseType=0

原始:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 14 Jan 2014 14:58:54 GMT
Content-Length: 36

{"ResponseType":0}{"ResponseType":1}

编辑 2:

得到它的工作所以这是现在的回应:

  HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 14 Jan 2014 15:15:28 GMT
Content-Length: 18

{"ResponseType":0}

我从 catch { } 中删除了代码,因为当将页面输出为 JSON 时,它弄乱了它并导致它被捕获。

现在它总是返回

  else {
$('#SuccessMsg').text('Test');
$('#SuccessMessage').modal('show');
}

而不是成功

想法?

最佳答案

我刚刚使用 JavascriptSerializer 类运行了一个测试来序列化一个枚举值,我收到了一个仅包含枚举值的整数表示的字符串(3 在我的案件)。我认为要获得有效的 json 字符串,您需要有一个对象。尝试这样的事情:

JsonConvert.SerializeObject(new { ResponseType = ReponseType.Success })

然后在 javascript 中:

data.ResponseType

关于c# - AJAX 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116160/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com