2016-12-02 103 views
-1

我的Ajax调用和操作方法看起来都正确,但看起来它正在返回HTML而不是有效的JSON。有人可以告诉我为什么我得到这个错误?AJAX意外令牌<位置4处的JSON

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); 
        } 
       }); 
      } 
     }); 
+0

它看起来像你*发送* HTML到你的服务器,它无法解析它,给我们你在服务器上看到什么? – xShirase

+0

我试过你的代码,它和我一起工作..什么是你的问题? –

+0

那么当您查看网络请求时,从服务器返回的内容是什么? – epascarello

回答

-3

你缺少catch块消息标签

+0

也许你可以展示如何用更新的代码片段修复这个问题? –

+0

public ActionResult UpdateStatus() { try { return Json(new {success = true,message =“success!”}); } catch(Exception ex) { return Json(new {success = false,message = ex.Message}); } } –

+0

将其编辑到您的答案中,不要只是将其作为评论。你的答案下面有一个“编辑”按钮。 –

-2

确保每一个输入具有ValidationMessageFor元

0

我最近得到了同样的错误,并改变dataType: "html"固定的错误。我的控制器方法正在返回一个局部视图,所以我的情况有点不同。