Jquery ajax 200 OK JSON.ParseError

I have a control that has a text box that when changing its contents will trick this javascript function:

page parameter document.URL because the control does not have an attached page .asxc and fieldValue is the value of the text field.

 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); } }); }; 

He continues to throw this error:

jqXHR: 200
textStatus: parsererror
errorThrown: SyntaxError: JSON.parse: unexpected character

IsSelectedListPictureLibrary Code:

 [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 tried changing json in ajax to jsonp , but the same error occurred.
I tried changing the data format.

Any ideas?

+4
source share
3 answers

Try to remove contentType and dataType from Ajax parameters and automatically identify them

+18
source

There was the same problem with the AJAX post team.

Sent a JSON request, received a 200 OK response, 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 that was not a JSON response.

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

+1
source

Not sure about [WebMethod], but it seems that there is a problem, and it is related to the output of this method. It must be well-formed JSON for the ajax method to work. So, what would I do is check the call in a separate window to see the answer and use something like http://jsonlint.com/ to make sure it is well-formed.

0
source

All Articles