【发布时间】:2015-08-20 16:14:46
【问题描述】:
我对 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>
现在我在 Ajax 中看到一个错误“未找到资源”,即使它执行了 MVC 方法
【问题讨论】:
-
没有
dataType: 'text',可能的选项是xml, json, script, or html。或者干脆一起删除,jqeury 会帮你解决的 -
dataType: 'html' 仍然存在同样的问题。
-
你在方法中添加了[HttpPost]属性吗?
-
添加了 HttpPost 是的。现在它给了我找不到资源。
-
您可以先返回一个字符串而不是 PartialView 来进行测试。
标签: c# ajax model-view-controller