Simple JsonResult returns parsererror on Jquery ajax method

天涯浪子 提交于 2019-12-11 07:37:41

问题


i'm using asp.net mvc2 with jquery 1.5.2. what i pretend is to make client-side call to a specific method in my controller that returns a json data.

Actually, my client-side call to server is working, but the problem is that jquery doesn't recognize the returned json.

I cannot understand what i'm doing wrong!, can some one helpme with this?

Controller method:

<HttpPost()> _
Function DoStuff(ByVal id As Integer) As JsonResult
    Dim retval As JsonResult = Nothing

    retval = Json(New xpto With {.P1 = "P1Value"})

    Return retval
End Function

JQuery Ajax call:

    $.ajax({
        url: "/DoStuff/5",
        type: "POST",
        dataType: "json",
        async: false,
        success: function(data, textStatus, jqXHR) {
            alert(textStatus);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus);
        }
    });

Values when Handles Error method:

errorThrown: jQuery1520029581770420782693_1303980789842 was not called

textStatus: parsererror

jqXHR.responseText = "{"P1":"P1Value"}"


回答1:


The problem was not in the on that code, that is working fine, the problem is generated by the Sctipt tag order on the markup (validate)

WRONG:

<script src="/Scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.maskedinput-1.3.min.js" type="text/javascript"></script>

RIGHT:

<script src="/Scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.maskedinput-1.3.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>


来源:https://stackoverflow.com/questions/5816126/simple-jsonresult-returns-parsererror-on-jquery-ajax-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!