我在提交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#仍然执行的方式(例如,它预定会议室)
有任何想法吗?
谢谢
编辑:
提琴手的回应
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句