我有簡單的頁面:阿賈克斯AspNetMvc錯誤請求
<div style="width:600px; margin-left:auto; margin-right:auto">
<div style="background-color: lightgray">
<h2>My Products</h2>
</div>
<p>Click the button to Get Products with an Ajax call</p>
<input id="btnAjax" name="btnAjax" type="button" value="Get Products" />
<div id="products" style="background-color:lightskyblue">
<div id='loadingmessage' style='display:none'>
loading...
</div>
</div>
</div>
和部分腳本:
@section Scripts {
@*@Scripts.Render("~/bundles/jqueryval")*@
<script>
$('#btnAjax').click(function() {
$.ajax({
url: '/Test/GetProducts',
contentType: 'application/html; charset=utf-8',
data: { id: 1 },
type: 'GET',
dataType: 'html'
})
.success(function (result) {
$('#products').html(result);
$('#loadingmessage').hide();
})
.error(function (xhr, status, throwError) {
alert(status);
})
});
</script>
}
中的TestController方法名的GetProducts(INT ID):
public PartialViewResult GetProducts(int id)
{
var listOfCourses = db.Courses.ToList();
Task.Delay(9000000);
if(id == 1)
throw new Exception("something bad");
return PartialView("_GetProducts", listOfCourses);
}
是任何在按鈕(#btnAjax)中設置'id'的方法並將其扔到ajax腳本中?例如:
<input id="btnAjax" name="btnAjax" type="button" value="Get Products" data:id="1" />
,然後在AJAX腳本
data: { id: $(this).id
讀它?
第二個問題是如何,我可以從異常錯誤事件得到消息錯誤(「壞事」)
「第二個問題是我如何從錯誤事件中的異常中獲取消息錯誤(」有些不好「)」我不理解這個問題,對不起。 – 2014-09-18 15:45:18
錯誤事件: .error(函數(XHR,狀態,throwError){ HERE } 怎麼這裏我可以把與法的GetProducts(INT ID) – W92 2014-09-18 15:47:23