C# AJAX-根据服务器响应的不同而做出不同的响应
这是我的C#代码: 让ajax请求知道它成功了C# AJAX-根据服务器响应的不同而做出不同的响应,c#,jquery,ajax,C#,Jquery,Ajax,这是我的C#代码: 让ajax请求知道它成功了 //Success appointment.RequiredAttendees.Add(roomEmail); appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy); } } catch
//Success
appointment.RequiredAttendees.Add(roomEmail);
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);
}
}
catch
{
}
}
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 () {
// Optionally alert the user of success here...
$('#BookRoom').modal('hide');
$('#SuccessMsg').text('Meeting Booked');
$('#SuccessMessage').modal('show');
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.
});
});
$(函数(){
$(“#BookButton”)。单击(函数(事件){
变量形式=$(“#形式1”);
$.ajax({
类型:form.attr('method'),
url:form.attr('action'),
数据:$(“#BookRoom:input”).serialize()
}).done(函数(){
//(可选)在此处提醒用户成功。。。
$(“#BookRoom”).modal('hide');
$(#successsg')。文本(“会议已预订”);
$('SuccessMessage').modal('show');
setTimeout(函数(){$('SuccessMessage').modal('hide');},3000);
}).fail(函数(){
//(可选)在此处警告用户错误。。。
警报(“提交AJAX请求时出错”);
});
event.preventDefault();//防止表单通过浏览器提交。
});
});
我的建议是响应一个枚举值。优势在于可扩展性: C# Javascript
var ResponseType = {
Success: 0,
InvalidInput: 1,
ServerError: 2,
....
}
在服务器上:
return base.Json(ReponseType.Success);
// or return base.Json(ReponseType.InvalidInput);
// or return base.Json(ReponseType.ServerError);
在客户机上:
$.ajax({
...
}).done(function (data) {
if (data === ResponseType.Success) {
// Notify user: Success
}
else if (data === ResponseType.InvalidInput) {
// Notify user: It is his fault
}
else if (data === ResponseType.ServerError) {
// Notify user: It is your fault
}
});
嗯。。。?那么你的问题是什么?老实说,我真的不想通过拖网浏览您的代码来尝试提出一个问题@下面的问题是如何从C#基本上将响应发送回AJAX。谢谢,我现在就开始,并让您知道我的进展:)Vanillebar Lutz C#代码产生错误:预期}此错误位于何处?你能通过pastebin为我提供一个更大的代码片段吗?pastebin.com/pu2vyatwt认为我已经修复了这个错误,现在getting System.Web.UI.Page不包含json的定义
var ResponseType = {
Success: 0,
InvalidInput: 1,
ServerError: 2,
....
}
return base.Json(ReponseType.Success);
// or return base.Json(ReponseType.InvalidInput);
// or return base.Json(ReponseType.ServerError);
$.ajax({
...
}).done(function (data) {
if (data === ResponseType.Success) {
// Notify user: Success
}
else if (data === ResponseType.InvalidInput) {
// Notify user: It is his fault
}
else if (data === ResponseType.ServerError) {
// Notify user: It is your fault
}
});