简体   繁体   中英

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. 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. My controller method is returning a partial view so my case is a little different.

确保每个输入都有ValidationMessageFor元素

您在catch块中缺少message标签

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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