简体   繁体   English

jquery ajax 200 OK JSON.ParseError

[英]jquery ajax 200 OK JSON.ParseError

I have a control which has a textbox which, when its content changes, will tricker this javascript function:我有一个控件,它有一个文本框,当它的内容发生变化时,它会欺骗这个 javascript 函数:

page parameter is document.URL as the control has no attached .asxc page and fieldValue is value of the textbox. page参数是document.URL ,因为控件没有附加的 .asxc 页面, fieldValue是文本框的值。

function UpdateFieldsOnListSelection(page, fieldValue) {
    $.ajax({
        type: "POST",
        url: page + "/IsSelectedListPictureLibrary",
        data: { "libraryInfo": fieldValue },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert("Success!");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("jqXHR: " + jqXHR.status + "\ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown);
        }
    });
};

It keeps throwing this error:它不断抛出这个错误:

jqXHR: 200 jqXHR: 200
textStatus: parsererror文本状态:解析器错误
errorThrown: SyntaxError: JSON.parse: unexpected character errorThrown: SyntaxError: JSON.parse: 意外字符

The code for IsSelectedListPictureLibrary : IsSelectedListPictureLibrary的代码:

[WebMethod]
public static bool IsSelectedListPictureLibrary(string libraryInfo)
{
    if (string.IsNullOrEmpty(libraryInfo)) return false;

    var common = new Utility();
    var storedLibraryInfo = common.GetStoredLibraryInfo(libraryInfo);

    if (storedLibraryInfo == null) return false;

    var web = SPContext.Current.Site.OpenWeb(storedLibraryInfo.WebId);
    var spList = web.Lists[storedLibraryInfo.LibraryId];

    if (spList.BaseTemplate == SPListTemplateType.PictureLibrary)
    {
        web.Dispose();
        return true;
    }

    web.Dispose();
    return false;
}

I have tried changing json in the ajax to jsonp , but same error occured.我尝试将 ajax 中的json更改为jsonp ,但发生了同样的错误。
I tried changing the format of data .我尝试更改data的格式。

Any ideas?有任何想法吗?

尝试从ajax参数中去掉contentTypedataType ,让它们自动识别

Had the same problem with AJAX's 'post' command. AJAX 的“发布”命令也有同样的问题。

Sent a JSON post request, got a 200 OK repsponse but textStatus was parseerror and errorThrown was SyntaxError: JSON.parse: unexpected character .发送 JSON 发布请求,得到 200 OK 响应,但 textStatus 是parseerror并且 errorThrown 是SyntaxError: JSON.parse: unexpected character

This is my JS code:这是我的 JS 代码:

$.post(url, JSON.stringify(reportVarsJson), function(response) {}, 'json')
.fail(function(jqXHR, textStatus, errorThrown) {
    alert('Error saving report request variables:\n\n' + jqXHR.responseText);
});

The problem turned out to be that my server view (Django) returned an empty response which was not a JSON response.问题原来是我的服务器视图(Django)返回了一个空响应,它不是 JSON 响应。

I changed my server view to return an empty json response and everything works well!我更改了我的服务器视图以返回一个空的 json 响应,并且一切正常!

Not sure about [WebMethod], but it seems that the problem is there, and it is related with the output of that method.不确定[WebMethod],但似乎问题在那里,并且与该方法的输出有关。 It has to be a well formed JSON for the ajax method to work.它必须是格式良好的 JSON 才能使 ajax 方法正常工作。 So what I would do is to check call that in a separate window to see the respons and to use something like http://jsonlint.com/ to make sure it is well formed.所以我要做的是在一个单独的窗口中检查调用以查看响应并使用http://jsonlint.com/之类的东西来确保它格式正确。

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

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