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
593 views
in Technique[技术] by (71.8m points)

webmethod - jquery ajax 200 OK JSON.ParseError

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 + "
textStatus: " + textStatus + "
errorThrown: " + 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?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


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

...