locked
Result: - parsererror : -SyntaxError: Unexpected token < in JSON at position 0:-200 :-OK RRS feed

  • Question

  • User-1686118291 posted
    <ArrayOfEmployee xmlns="http://schemas.datacontract.org/2004/07/CR.WebServices.Model.EntityFramework" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Employee>
    	<Email>fnln@mycompany.com</Email>
    	<EmployeeCode>12345</EmployeeCode>
    	<EmployeeName>xyz,pqr</EmployeeName>
    </Employee>
    <Employee>
    	<Email>fnln1@mycompany.com</Email>
    	<EmployeeCode>12346</EmployeeCode>
    	<EmployeeName>hij,klm</EmployeeName>
    </Employee>
    </ArrayOfEmployee>

    I have a REST API service http://services.mycompany.com/Employees/ and browse this service results XML as shown above. I want to read via Ajax/Json call. I added a simple script

    $.getJSON('http://services.mycompany.com/Employees/'
              ).done(function (result, status, xhr) {
    		alert(result.length);
    	  }).fail(function (xhr, status, error) {
    		alert("Result: " + status + " : " + error + ":" + xhr.status + " :" + xhr.statusText)
    	  });  

    The output alert always throw the following result:
    Result: parsererror : SyntaxError: Unexpected token < in JSON at position 0 : 200 :OK

    Will you please help troubleshooting? Do I have to convert xml to JSON? I verified in Fiddler debugging and I see that the 'url' has the complete xml value however it never produce result and always display the parseerror as stated.
    All I need is total employees returned from the web service count. In the above case, it should alert me as "2". Any other easiest way to get the result would also work but it should work in .aspx page.



    Friday, August 21, 2020 7:36 PM

Answers

  • User-474980206 posted

    The response is xml, not json. As .getJSON() sends the accept json header, it expects a Json result. , it appears you rest api does not support json. So you need to Use $.get(). the result of the $.get() will be an xml dom. See jquery support of xml to access your data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 22, 2020 3:06 PM

All replies

  • User-474980206 posted

    The response is xml, not json. As .getJSON() sends the accept json header, it expects a Json result. , it appears you rest api does not support json. So you need to Use $.get(). the result of the $.get() will be an xml dom. See jquery support of xml to access your data.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 22, 2020 3:06 PM
  • User521825913 posted

    You're receiving HTML (or XML) back from the server, but the dataType: json is telling jQuery to parse as JSON. Check the "Network" tab in Chrome dev tools to see contents of the server's response.

    I would suggest that you add to your logging. Looking at the actual xhr (XMLHttpRequest) properties to learn more about the response. Try adding console.warn(xhr.responseText) and you will most likely see the HTML that is being received.

    Monday, August 24, 2020 6:12 AM