Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token < in JSON at position 4

var url="http://fsa.citop.in/lnct/service/signProcess.aspx";
var data={txtLogId: "[email protected]",txtLogPass: "xyz",hdnReqType2: "sign87162"};
var success=function(data, textStatus, jqXHR) {
            console.log(data);
         };

var fail=function(jqXHR, textStatus, errorThrown) {
         console.log("Error:" + errorThrown );
     }
$.ajax({
  type: "POST",
  url: url,
  data:data,
  success:success,
  error:fail,

});

This POST request gives me the error, SyntaxError: Unexpected token < in JSON at position 4, in the console of the page 'http://fsa.citop.in/lnct/' in chrome.

But if I use fsa.citop.in/lnct/service/signProcess.aspx (i.e. no http://), it gives me no error, but nothing comes back in data. On success of POST request, a JSON object is expected. Please somebody explain what is happening here and how it could be resolved.

like image 973
Himanshu Singh Avatar asked Apr 23 '16 18:04

Himanshu Singh


People also ask

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What does unexpected token h in JSON at position 0 mean?

Re: Unexpected token in JSON at position 0 This usually means that an error has been returned and that's not valid JSON. Check the browser developer tools console and network tabs. Turn on Debugging and (after reproducing the error) check the web server error logs.

How do you handle JSON parsing error?

The most common way to handle JSON parse error is using try-catch block. If the JSON string is valid, it will return a JavaScript object. If the JSON string is invalid, it will throw a SyntaxError.

What does position mean in JSON?

The position in the error message indicates which byte within the file the error occurs around.


2 Answers

For those who encounter this problem in AWS Lambda code editor it is most likely your session has timed out.

Try reloading the page and signing in again. It should resolve this.

like image 127
David Salamon Avatar answered Sep 27 '22 21:09

David Salamon


It's most likely because the response is HTML and it's trying to parse it as something else. The < at position 4 is the first < of <!DOCTYPE html....

You should try to specify dataType in your ajax call (see http://api.jquery.com/jquery.ajax/) and also make signProcess.aspx to return something more useful (currently the response content type is application/json but it prints HTML).

like image 22
Gabriel Avatar answered Sep 27 '22 21:09

Gabriel