我需要從控制器發送一個錯誤消息,以防ajax函數中發生錯誤。 這裏是我的控制器代碼:從控制器發送字符串到錯誤ajax函數MVC3
//check if the day is already approved
string check1 = "SELECT approve_status_id FROM table_approvals WHERE approve_date=" + date + " AND user_id=" + Session["userID"];
MySqlCommand status_cmd = new MySqlCommand(check1, con);
MySqlDataReader rdrStatus = status_cmd.ExecuteReader();
while (rdrStatus.Read())
{
if (rdrStatus.GetString(0) == "5")
{
valid = false;
error = "You cannot edit already approved dates!";
response.Message = error;
return View(Json(new { success = false,error},JsonRequestBehavior.AllowGet));
}
}
我AJAX功能:
$.ajax({
url: '@Url.Action("SaveLine", "AddLine")',
type: 'post',
data: { ids: IDs },
dataType: 'json',
traditional: true,
success: function() {
window.location.href = '@Url.Action("Index", "AddLine")';
},
error: function() {
$.getJSON('/AddLine/SaveLine', function (json) {
alert('error');
$("#ajaxPostMessage").html(json.Message);
});
您可能想了解[SQL Injection](http://www.securiteam.com/securityreviews/5DP0N1P76E.html) –