简体   繁体   English

AJAX意外令牌<JSON在位置4

[英]AJAX Unexpected Token < JSON at position 4

Everything looks correct with my Ajax call and action method but looks like it's returning HTML, instead of valid JSON. 使用我的Ajax调用和操作方法,一切看起来都正确,但是看起来它正在返回HTML,而不是有效的JSON。 Can someone tell me why I'm getting this error? 有人可以告诉我为什么出现此错误吗?

Error: parsererror SyntaxError: Unexpected token < in JSON at position 4

[HttpPost]
    public ActionResult UpdateStatus()
    {
        try
        {
            return Json(new { success = true, message = "success!" });
        }
        catch (Exception ex)
        {
            return Json(new { success = false, ex.Message });
        }
    }

        $("#updateStatus").click(function () {
                $.ajax({
                    url: "@Url.Action("UpdateStatus","Home")",
                    type: "post",
                    dataType: "json",
                    cache: false,
                    success: function (response, textStatus, jqXHR) {

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert("Error: " + textStatus + " " + errorThrown);
                    }
                });
            }
        });

I recently got the same error and changing dataType: "html" fixed the error. 我最近遇到了相同的错误,并更改了dataType: "html"修复了该错误。 My controller method is returning a partial view so my case is a little different. 我的控制器方法返回部分视图,因此我的情况有些不同。

确保每个输入都有ValidationMessageFor元素

您在catch块中缺少message标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM