![](/img/trans.png)
[英]JQuery ajax call to MVC action always returns an error when there isn't one
[英]Simple Ajax Call to MVC method always returns error
我對MVC方法有一個非常簡單的ajax調用,它總是返回帶有空白responseText的錯誤。 我已經使用ajax多年了,似乎無法弄清楚是什么原因造成的:
這是js代碼:
$.ajax({
url: "/Mobile/MyMVCMethod",
data: null,
type: "POST",
dataType: 'html',
success: function (data) {
alert("test");
},
error: function (request, status, error) {
alert(status);
alert(request.responseText);
alert(error);
}
});
這是我的C#:
[HttpPost]
public string MyMVCMethod()
{
return "test";
}
代碼將其傳遞給方法,但返回沒有詳細信息的錯誤
我嘗試了ActionResult是因為我想返回PartialView,但是由於它不起作用,我試圖縮小范圍並找出是否僅適用於字符串。 即使C#是這樣,這也在發生:
[HttpPost]
public ActionResult MyMVCMethod()
{
return PartialView("MyView");
}
我的觀點:
<div>hello world</div>
現在,即使執行了MVC方法,我仍然在Ajax中看到“找不到資源”錯誤
$.ajax({
url:"Mobile/MyMethod",
method:"post",
datatype:"json"
}).done(function( data ) {
alert( "Data " + data );
});
並確保僅調用普通方法而不是操作結果。然后嘗試它可能對您有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.