Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

javascript - jQuery为ajax请求返回“ parsererror”(jQuery returning “parsererror” for ajax request)

Been getting a "parsererror" from jquery for an Ajax request, I have tried changing the POST to a GET, returning the data in a few different ways (creating classes, etc.) but I cant seem to figure out what the problem is.

(我一直在从jquery收到针对Ajax请求的“ parsererror”,我尝试将POST更改为GET,以几种不同的方式(创建类等)返回数据,但我似乎无法弄清楚问题出在哪里。)

My project is in MVC3 and I'm using jQuery 1.5 I have a Dropdown and on the onchange event I fire off a call to get some data based on what was selected.

(我的项目在MVC3中,我使用的是jQuery 1.5,我有一个Dropdown,并且在onchange事件上,我触发了一个调用,以根据所选内容获取一些数据。)

Dropdown: (this loads the "Views" from the list in the Viewbag and firing the event works fine)

(下拉列表:(这会从Viewbag的列表中加载“ Views”,并触发事件可以正常进行))

@{
    var viewHtmls = new Dictionary<string, object>();
    viewHtmls.Add("data-bind", "value: ViewID");
    viewHtmls.Add("onchange", "javascript:PageModel.LoadViewContentNames()");
}
@Html.DropDownList("view", (List<SelectListItem>)ViewBag.Views, viewHtmls)

Javascript:

(Javascript:)

this.LoadViewContentNames = function () {
    $.ajax({
        url: '/Admin/Ajax/GetViewContentNames',
        type: 'POST',
        dataType: 'json',
        data: { viewID: $("#view").val() },
        success: function (data) {
            alert(data);
        },
        error: function (data) {
            debugger;
            alert("Error");
        }
    });
};

The above code successfully calls the MVC method and returns:

(上面的代码成功调用了MVC方法并返回:)

[{"ViewContentID":1,"Name":"TopContent","Note":"Content on the top"},
 {"ViewContentID":2,"Name":"BottomContent","Note":"Content on the bottom"}]

But jquery fires the error event for the $.ajax() method saying "parsererror".

(但是jquery触发$ .ajax()方法的错误事件,提示“ parsererror”。)

  ask by dkarzon translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I recently encountered this problem and stumbled upon this question.

(我最近遇到了这个问题,偶然发现了这个问题。)

I resolved it with a much easier way.

(我用一种简单得多的方法解决了它。)

Method One

(方法一)

You can either remove the dataType: 'json' property from the object literal...

(您可以从对象文字中删除dataType: 'json'属性...)

Method Two

(方法二)

Or you can do what @Sagiv was saying by returning your data as Json .

(或者您可以通过将数据返回为Json来执行@Sagiv所说的。)


The reason why this parsererror message occurs is that when you simply return a string or another value, it is not really Json , so the parser fails when parsing it.

(发生此parsererror消息的原因是,当您仅返回一个字符串或另一个值时,它实际上不是Json ,因此解析器在解析它时会失败。)

So if you remove the dataType: json property, it will not try to parse it as Json .

(因此,如果删除dataType: json属性,它将不会尝试将其解析为Json 。)

With the other method if you make sure to return your data as Json , the parser will know how to handle it properly.

(使用其他方法,如果您确保将数据作为Json返回,则解析器将知道如何正确处理它。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...