jquery ajax 200 OK JSON.ParseError
Asked Answered
H

3

5

I have a control which has a textbox which, when its content changes, will tricker this javascript function:

page parameter is document.URL as the control has no attached .asxc page and fieldValue is value of the textbox.

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
textStatus: parsererror
errorThrown: SyntaxError: JSON.parse: unexpected character

The code for 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.
I tried changing the format of data.

Any ideas?

Hiro answered 15/4, 2013 at 10:21 Comment(4)
What does the response look like?Bathhouse
Try to remove contentType and dataType from Ajax parameters and let them be identified automaticallyEchikson
@RaraituL This worked. Can you please post your comment as an answer so I can make it as answer.Hiro
@Daniel Ziga: Great..i will do it nowEchikson
E
23

Try to remove contentType and dataType from Ajax parameters and let them be identified automatically

Echikson answered 15/4, 2013 at 10:40 Comment(2)
I have worked for 2 days trying EVERYONE's "solution" to this problem. Your suggestion was the one that fixed it for me. Thank you very much.Disjointed
@Disjointed i'm glad it helped you!Echikson
S
1

Had the same problem with AJAX's 'post' command.

Sent a JSON post request, got a 200 OK repsponse but textStatus was parseerror and errorThrown was SyntaxError: JSON.parse: unexpected character.

This is my JS code:

$.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.

I changed my server view to return an empty json response and everything works well!

Sweetmeat answered 24/4, 2014 at 13:19 Comment(0)
J
0

Not sure about [WebMethod], but it seems that the problem is there, and it is related with the output of that method. It has to be a well formed JSON for the ajax method to work. 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.

Janeanjaneczka answered 15/4, 2013 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.