【发布时间】:2015-05-23 07:17:01
【问题描述】:
我有一个像下面这样的 jquery ajax 脚本
$.ajax({
type: "POST",
url: "Main/receive", // the method we are calling
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ 'p':$("#txtname").val() }),
dataType: "json",
success: function (result) {
alert('Yay! It worked!');
// Or if you are returning something
},
error: function (result) {
alert('Oh no zzzz:('+result.responseText);
}
});
我正在调用 Controller 操作方法。数据正在发送到控制器的操作方法,我也从控制器接收数据。但是我收到的数据在 jquery ajax 的错误函数中。
我希望它在成功函数中。
为什么我的成功函数没有被调用。
以下是我的控制器的动作方法,
[HttpPost]
public string receive(string p)
{
ViewBag.name = p;
return p;
}
【问题讨论】:
-
因为您已指定返回类型为 json(即
dataType: "json",)。将服务器方法更改为return Json(p);但是您的代码中有很多或其他潜在错误,所以我稍后会发布答案。 -
@StephenMuecke 谢谢,请不要忘记发布答案
标签: c# jquery .net ajax asp.net-mvc