【问题标题】:Ajax never fires 'success' using ASP.NETAjax 永远不会使用 ASP.NET 触发“成功”
【发布时间】:2013-08-04 04:47:06
【问题描述】:

我正在尝试执行一个简单的 Ajax 调用来从 HTML 页面注册用户,MVC 4 中的函数被调用并且运行良好,但它返回的内容永远不会触发 Ajax 调用中的“成功”函数。

如果我使用浏览器手动访问 Register() 函数,它运行良好并返回我想要的消息,但不是通过 Ajax

function register() {
    var userModel = {
        phoneNumber: "1236663",
        displayname: "yasuuu",
    }
    $.ajax({
        type: "POST",
        url: "http://localhost:13234/home/register",
        data: userModel,
        success: function (response) {
            $('#deleteThisDivButNotItsContent').html(response)
        }
    })
}

    public ActionResult Register(string PhoneNumber, string DisplayName)
    {
        // Some working code here ...

        ViewBag.Message = "Some Message"; // just trying to display simple text

        return View();

        /* tried all of the lines below too */

        //return this.Json("1234");
        //return Json("{ result : true }");
        //return PartialView("ShowResultPartial", JsonRequestBehavior.AllowGet);
        //CommentSection commentSection = new CommentSection();
        //return PartialView("CommentSection");
        //return PartialView("CommentSection", commentSection);
        //return Json(new { success = true, response = "Saved ok" });
    }

我正在使用 JQuery 2.0.3

【问题讨论】:

  • 我已经在互联网上搜索了几个小时,但找不到任何东西:(
  • 您使用的是哪个版本的 jQuery?您应该使用带有.done(...) 的承诺/延迟样式,而不是使用success
  • 我认为成功已经过时了。尝试完成。 http://api.jquery.com/jQuery.ajax/
  • 使用 .done(...) 它不起作用。但是当我像 Dmytro 所说的那样使用完整时,它确实进入了 .complete(function(data) 的主体,当我使用 JSON.stringify(data) 解析返回参数时,它变成 {"readyState":4,"status ":404,"statusText":"错误"}
  • 您的浏览器控制台是否出现错误?

标签: c# asp.net-mvc jquery actionresult


【解决方案1】:

让它返回JsonResult 而不是ActionResult。 AJAX 到底如何解释 ActionResult 视图?

您还向 Register() 方法发送了一个对象,但您收到了两个字符串变量。

这是一个例子:

function register() {
    var userModel = {
        phoneNumber: "1236663",
        displayName: "yasuuu",
    }

    $.ajax({
        type: "POST",
        url: "http://localhost:13234/Home/Register",
        data: userModel,
        success: function (response) {
            $('#deleteThisDivButNotItsContent').html(response.message)
        }
    });
}

publi class UserModel
{
    public string phoneNumber { get; set; }
    public string displayName { get; set; }
}

public JsonResult Register(UserModel userModel)
{
    // Some working code here ...

    return Json(new { message = "Some message here" }, JsonRequestBehavior.AllowGet);
}

【讨论】:

  • 嘿,这里的结果相同。 {"readyState":4,"status":404,"statusText":"error"}
【解决方案2】:

你为什么不试试这个?

function register() {
$.post("/home/Register",
       {phoneNumber: "1236663", displayname: "yasuuu"},
       function(data,status,xhr){
            $('#deleteThisDivButNotItsContent').html(data.response);
});
}

在控制器中

return Json(new { success = true, response = "Saved ok" });

【讨论】:

  • 嘿,我已经使用了您发布的内容,使用我的浏览器,对 URL 和 AllowGet 进行了一些修改,但仍然没有运气:$.post("localhost:13234/home/Register", {phoneNumber:" 1236663",显示名称:"yasuuu"}) .done(function(data) { $('#deleteThisDivButNotItsContent').html(data); }); return Json(new { success = true, response = "Saved ok" }, JsonRequestBehavior.AllowGet);
  • 尝试使用浏览器访问“localhost:13234/home/Register”。你能看到一个json字符串什么的吗?
  • 使用浏览器,它正在工作。我要去localhost:13234/home/… 取回 JSON {"success":true,"response":"Saved ok"}
  • 还是不行,Register() 以调试模式启动,但现在 html 中没有任何变化。
  • 用一个简单的 alert() 我看到函数(数据、状态、xhr)没有启动。不像“完成”,它启动了但有“404”
【解决方案3】:

当您在浏览器中键入 http://localhost:1234/register 时,它会发出对该 URL 的 GET 请求。在您的 javascript 中,jquery 发出 POST 请求。 ajax 请求收到 404 错误,因为路由未配置为响应 POST 请求。

【讨论】:

  • 我在MVC的Register函数前加了[HttpPost],但是还是不行,是这个意思吗?
  • 这就是我的意思。您是否正在使用浏览器调试工具查看网络请求?也许您可以尝试 cUrl (curl.haxx.se) - 问题是您的网站正在向 ajax 帖子返回 HTTP404(未找到)响应。你只需要弄清楚为什么。
  • 但是当我使用浏览器调用 localhost:1283/home/… 时,它工作正常。而当我使用 Ajax 时,问题就来了。知道为什么会发生这种情况吗? :\
  • 尝试使用 Ajax 发出 GET 请求(即 $.get("/whatever")),或使用 CURL 发出 POST 和 GET 请求,看看是否有问题。将 ajax 调用与在浏览器中键入 URL 进行比较是不公平的比较
  • 更改为 $.get(..) 并没有解决问题。我也尝试在带有 IIS 8 的 Windows 8 上运行它,同样的问题。 CURL 看起来很复杂:/
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 2018-10-14
  • 1970-01-01
  • 2011-04-01
相关资源
最近更新 更多