"Uncaught SyntaxError: Unexpected token <in JSON" Error in JSON.parse on Ajax

Asked 2 years ago, Updated 2 years ago, 110 views

We have implemented a client-side program as a test for communication to the API server.
However, the contents of the questionnaire could not be sent to the API server, so the results page
It doesn't even come out.

Anyway, when I google the error content, there are similar errors, but the content is a little different. As a beginner, I didn't understand at all.Should I have studied a little more?

Also, even if you run source code line by line on the console, there are no errors at that time.
Yes, I tried 'var_dump('foo');' and so on, but I couldn't find any bugs.

The console error is Uncaught SyntaxError: Unexpected token <in JSON
Then at position 0 at JSON.parse(<anonymous>) and
at XMLHttpRequest.xmlhttp.onreadystatechange(awstest.php:181)
This is what it says.

Source Code: Lines 175 to 192

function postForm(form){
  // xmlhttp = new XMLHttpRequest();
  var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4&xmlhttp.status==200){
      var response=JSON.parse(xmlhttp.responseText);
      console.log(response);
      showResult (response);
    }
  }

var formelement= document.querySelector("#form1");
  var form_data = new FormData(formelement);
  xmlhttp.open('POST',analizepage,true);
  xmlhttp.send(form_data);

  return false;
}

The line with the error is line 181 with var response=JSON.parse(xmlhttp.responseText);.

If you have any solutions or clues, please let me know.

April 20, 2017 23:10 Add:

The contents of xmlhttp.responseText were arrays of such associative arrays.
However, there was a warning, so I will verify the analysis page on the API server side.

<br/>
<b>Warning</b>:PDOSstatement:::execute():SQLSTATE [HY093]:Invalid parameter number:parameter was not defined in <b>/var/www/html/dist/hqcck/analyze.php</bt>bt>11>bt>74.
{"result":{"nutrients":[{"name":"\u30ab\u30b\u30b7\u30a6\u30e0", "value":"6800"}, {"name":"\u30de\u30b0\u30cd\u30b7\u30a6\u30e0", "value":"6400"}, {"name"\u30"\u30"\u30"\u30"},{"\u30"\u30"\u30"\u30"\u30"\u30"\u3nt_sufficiency_number":{"name":"\u25cf\u25cf\u25cf \u6804\u990a\u5145\u8db3\u5ea6 \u25cf\u25cf\u25cf","value":"82.2"},"nutrient_comment":{"name":"\u25cf\u25cf\u25cf \u4e0d\u8db3\u6804\u990a\u7d20\u30b3\u30e1\u30f3\u30c8 \u25cf\u25cf\u25cf","value":"\u3053\u306e\u6804\u990a\u7d20\u306f\u3001\u3042\u306a\u305f\u306e\u8eab\u4f53\u3068\u5fc3\u304c\u5fc5\u8981\u3068\u3057\u3066\u3044\u308b\u4f53\u5185\u9175\u7d20\u3092\u88dc\u5b8c\u3059\u308b\u30d3\u30bf\u30f3\u30fb\u30df\u30cdf\u30e9\u309\u309\u309\u309\u309\u309\u309\u309\u309\u309\u309\8\u306b\u3088\u3063\u3066\u3001\u7cbe\u795e\u7d4c\u7cfb\u306e\u8abf\u548c\u3092\u3068\u308b\u305f\u308b\u308b\u5fc5\u8981\u306a\u30a\u30a8\u30a8\u30cd\u30a8\u30d\u30d\u30a\u30d\u30\uu307e\u3059\u3002\u3053\u306e\u4e8b\u306b\u3088\u308a\u3001\u4fbf\u79d8\u3084\u98df\u6b32\u4e0d\u632f\u3001\u4e0b\u75e2\u3001\u51b7\u3048\u75c7\u3001\u52d5\u60b8\u606f\u5207\u308c\u7b49\u3001\u7a2e\u3005\u306e\u75c7\u72b6\u304c \u91cd\u306a\u3063\u305f\u4e0d\u5b9a\u6101\u8a34\u3068\u3057\u3066\u8868\u9762\u5316\u3057\u3066\u304f\u308b\u4e8b\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u3002"},"sym_trend_1":{"name":"\u25cf\u25cf\u25cf \u75be\u75c5\u50be\u5411 \u25cf\u25cf\u25cf  \u81ea\u5f8b\u795e\u7d4c\u30a4\u30f3\u30d0\u30e9\u30f3\u30b9","value":"51.7"},"exhaustion_number":{"name":"\u25cf\u25cf\u25cf \u30b9\u30c8\u30ec\u30b9\u30fb\u75b2\u52b4\u5ea6 \u25cf\u25cf\u25cf","value":"18.2"},"nutrient_activation_number":{"name":"\u25cf\u25cf\u25cf \u6804\u990a\u6d3b\u6027\u5ea6 \u25cf\u25cf\u25cf","value":"29"},"BMI":{"name":"\u25cf\u25cf\u25cf BMI \u25cf\u25cf\u25cf","value":"21"},"power_number":{"name":"\u25cf\u25cf\u25cf \u4f53\u5185\u6d3b\u529b\u6307\u6570 \u25cf\u25cf\u25cf","value":"68"},"depression_value":{"name":"\u25cf\u25cf\u25cf \u3046\u3064\u4fc2\u6570 \u25cf\u25cf\u25cf","value":"8"},"aheadsick_type":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u30bf\u30a4\u30d7 \u25cf\u25cf\u25cf","value":"4"},"mibyoudo":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u5ea6 \u25cf\u25cf\u25cf","value":"4"},"power_area":{"name":"\u25cf\u25cf\u25cf \u30ab\u30e9\u30c0\u3068\u30b3\u30b3\u30ed\u306e\u30d0\u30e9\u30f3\u30b9\u30a8\u30ea\u30a2 \u25cf\u25cf\u25cf","value":"A"},"msg_bmi":{"name":"\u25cf\u25cf\u25cf BMI\u72b6\u614b \u25cf\u25cf\u25cf","value":"BMI\u6307\u6570\u304c18.5\u4ee5\u4e0a\u304b\u308925\u672a\u6e80\u306f\u300c\u666e\u901a\u4f53\u91cd\u300d\u306e\u7bc4\u56f2\u3067\u3001\u6307\u657022\u304c\u6a19\u6e96\u5024\u3068\u3055\u308c\u3066\u3044\u307e\u3059\u3002"},"tokuchou":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u30bf\u30a4\u30d7\u7279\u5fb4 \u25cf\u25cf\u25cf","value":"\u300c\u6804\u990a\u5145\u8db3\u5ea6\u300d\u306f\u6a19\u6e96\u7bc4\u56f2\u5024\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u4f53\u5185\u306e\u6804\u990a\u7d20\u306f\u5145\u8db3\u3055\u308c\u3066\u3044\u308b\u72b6\u614b\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u3002\u8eab\u4f53\u304c\u611f\u53d7\u3059\u308b\u30b9\u30c8\u30c8\u30b9\u306b\u306bfe\u3057\u3001\u642\u3055c\u305f\u305f\u804\u609\u309\u309\u309\u309\u309\u309\u306\\u3044\u308b\u304b\u3092\u6570\u5024\u5316\u3057\u3066\u8868\u3057\u305f\u3082\u306e\u304c\u300c\u6804\u3a\u6d3b\u6027\u5ea6\u3067\u3059\u3042\u306a\u306\u306\u306\u306\u306\u306\u306\u306\u3053055\u308c\u305f\u6804\u990a\u7d20\u304c\u4f53\u5185\u3067\u6709\u52b9\u6d3b\u7528\u3055\u308c\u3066\u3044\u306a\u3044\u72b6\u614b\u304c\u3046\u304b\u304c\u3048\u307e\u3059\u3002"},"cd_risk":{"name":"\u25cf\u25cf\u25cf \u8a8d\u77e5\u6a5f\u80fd\u30ea\u30b9\u30af\u5ea6 \u25cf\u25cf\u25cf","value":"42.3"},"cd_area":{"name":"\u25cf\u25cf\u25cf \u8a8d\u77e5\u6a5f\u80fd\u30ea\u30b9\u30af\u30a8\u30ea\u30a2\u25cf\u25cf\u25cf", "value": "B1"}}}}

javascript jquery json api ajax

2022-09-30 18:09

3 Answers

Uncaught SyntaxError: Unexpected token <in JSON

In short, the string given to JSON.parse() is not the correct JSON.< is probably HTML in the API response?

For example, console.log(xmlhttp.responseText) in front of JSON.parse() and output the contents of the response to the console.


2022-09-30 18:09

April 20, 2017 23:10 Add:

The contents of xmlhttp.responseText were arrays of such associative arrays.
However, there was a warning, so I will verify the analysis page on the API server side.

<br/>
<b>Warning</b>:PDOSstatement:::execute():SQLSTATE [HY093]:Invalid parameter number:parameter was not defined in <b>/var/www/html/dist/hqcck/analyze.php</bt>bt>11>bt>74.
{"result":{"nutrients":[{"name":"\u30ab\u30b\u30b7\u30a6\u30e0", "value":"6800"}, {"name":"\u30de\u30b0\u30cd\u30b7\u30a6\u30e0", "value":"6400"}, {"name"\u30"\u30"\u30"\u30"},{"\u30"\u30"\u30"\u30"\u30"\u30"\u3nt_sufficiency_number":{"name":"\u25cf\u25cf\u25cf \u6804\u990a\u5145\u8db3\u5ea6 \u25cf\u25cf\u25cf","value":"82.2"},"nutrient_comment":{"name":"\u25cf\u25cf\u25cf \u4e0d\u8db3\u6804\u990a\u7d20\u30b3\u30e1\u30f3\u30c8 \u25cf\u25cf\u25cf","value":"\u3053\u306e\u6804\u990a\u7d20\u306f\u3001\u3042\u306a\u305f\u306e\u8eab\u4f53\u3068\u5fc3\u304c\u5fc5\u8981\u3068\u3057\u3066\u3044\u308b\u4f53\u5185\u9175\u7d20\u3092\u88dc\u5b8c\u3059\u308b\u30d3\u30bf\u30f3\u30fb\u30df\u30cdf\u30e9\u309\u309\u309\u309\u309\u309\u309\u309\u309\u309\u309\8\u306b\u3088\u3063\u3066\u3001\u7cbe\u795e\u7d4c\u7cfb\u306e\u8abf\u548c\u3092\u3068\u308b\u305f\u308b\u308b\u5fc5\u8981\u306a\u30a\u30a8\u30a8\u30cd\u30a8\u30d\u30d\u30a\u30d\u30\uu307e\u3059\u3002\u3053\u306e\u4e8b\u306b\u3088\u308a\u3001\u4fbf\u79d8\u3084\u98df\u6b32\u4e0d\u632f\u3001\u4e0b\u75e2\u3001\u51b7\u3048\u75c7\u3001\u52d5\u60b8\u606f\u5207\u308c\u7b49\u3001\u7a2e\u3005\u306e\u75c7\u72b6\u304c \u91cd\u306a\u3063\u305f\u4e0d\u5b9a\u6101\u8a34\u3068\u3057\u3066\u8868\u9762\u5316\u3057\u3066\u304f\u308b\u4e8b\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u3002"},"sym_trend_1":{"name":"\u25cf\u25cf\u25cf \u75be\u75c5\u50be\u5411 \u25cf\u25cf\u25cf  \u81ea\u5f8b\u795e\u7d4c\u30a4\u30f3\u30d0\u30e9\u30f3\u30b9","value":"51.7"},"exhaustion_number":{"name":"\u25cf\u25cf\u25cf \u30b9\u30c8\u30ec\u30b9\u30fb\u75b2\u52b4\u5ea6 \u25cf\u25cf\u25cf","value":"18.2"},"nutrient_activation_number":{"name":"\u25cf\u25cf\u25cf \u6804\u990a\u6d3b\u6027\u5ea6 \u25cf\u25cf\u25cf","value":"29"},"BMI":{"name":"\u25cf\u25cf\u25cf BMI \u25cf\u25cf\u25cf","value":"21"},"power_number":{"name":"\u25cf\u25cf\u25cf \u4f53\u5185\u6d3b\u529b\u6307\u6570 \u25cf\u25cf\u25cf","value":"68"},"depression_value":{"name":"\u25cf\u25cf\u25cf \u3046\u3064\u4fc2\u6570 \u25cf\u25cf\u25cf","value":"8"},"aheadsick_type":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u30bf\u30a4\u30d7 \u25cf\u25cf\u25cf","value":"4"},"mibyoudo":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u5ea6 \u25cf\u25cf\u25cf","value":"4"},"power_area":{"name":"\u25cf\u25cf\u25cf \u30ab\u30e9\u30c0\u3068\u30b3\u30b3\u30ed\u306e\u30d0\u30e9\u30f3\u30b9\u30a8\u30ea\u30a2 \u25cf\u25cf\u25cf","value":"A"},"msg_bmi":{"name":"\u25cf\u25cf\u25cf BMI\u72b6\u614b \u25cf\u25cf\u25cf","value":"BMI\u6307\u6570\u304c18.5\u4ee5\u4e0a\u304b\u308925\u672a\u6e80\u306f\u300c\u666e\u901a\u4f53\u91cd\u300d\u306e\u7bc4\u56f2\u3067\u3001\u6307\u657022\u304c\u6a19\u6e96\u5024\u3068\u3055\u308c\u3066\u3044\u307e\u3059\u3002"},"tokuchou":{"name":"\u25cf\u25cf\u25cf \u672a\u75c5\u30bf\u30a4\u30d7\u7279\u5fb4 \u25cf\u25cf\u25cf","value":"\u300c\u6804\u990a\u5145\u8db3\u5ea6\u300d\u306f\u6a19\u6e96\u7bc4\u56f2\u5024\u3092\u793a\u3057\u3066\u304a\u308a\u3001\u4f53\u5185\u306e\u6804\u990a\u7d20\u306f\u5145\u8db3\u3055\u308c\u3066\u3044\u308b\u72b6\u614b\u304c\u8003\u3048\u3089\u308c\u307e\u3059\u3002\u8eab\u4f53\u304c\u611f\u53d7\u3059\u308b\u30b9\u30c8\u30c8\u30b9\u306b\u306bfe\u3057\u3001\u642\u3055c\u305f\u305f\u804\u609\u309\u309\u309\u309\u309\u309\u306\\u3044\u308b\u304b\u3092\u6570\u5024\u5316\u3057\u3066\u8868\u3057\u305f\u3082\u306e\u304c\u300c\u6804\u3a\u6d3b\u6027\u5ea6\u3067\u3059\u3042\u306a\u306\u306\u306\u306\u306\u306\u306\u306\u3053055\u308c\u305f\u6804\u990a\u7d20\u304c\u4f53\u5185\u3067\u6709\u52b9\u6d3b\u7528\u3055\u308c\u3066\u3044\u306a\u3044\u72b6\u614b\u304c\u3046\u304b\u304c\u3048\u307e\u3059\u3002"},"cd_risk":{"name":"\u25cf\u25cf\u25cf \u8a8d\u77e5\u6a5f\u80fd\u30ea\u30b9\u30af\u5ea6 \u25cf\u25cf\u25cf","value":"42.3"},"cd_area":{"name":"\u25cf\u25cf\u25cf \u8a8d\u77e5\u6a5f\u80fd\u30ea\u30b9\u30af\u30a8\u30ea\u30a2\u25cf\u25cf\u25cf", "value": "B1"}}}}

If this is the JSON output, there seems to be a problem with the code related to the JSON output process.
The error can be reproduced by executing the code below.

JSON.parse('<br/>');
// Uncaught SyntaxError: Unexpected token <in JSON at position 0
//    at JSON.parse (<anonymous>)
//    at<anonymous>—1:6

The problem is that Warning is printed and JSON is destroyed, so I think the code part needs to be corrected.


2022-09-30 18:09

I think it has already been resolved, but since the warning is output during php execution, json parse error is generated mixed with json data.

Check the parameters passed to php for defects (for example, form parameter name error).
There may be a flaw in the parameters passed and the php "Warning" is printed.

If the parameters you pass are correct, you can request a correction of /var/wwww/html/dist/hqcck/analyze.php to prevent the php "Warning" from being printed, or separate the warning and json data by split as follows:

str_array=in_string.split('<br/>');
var response=JSON.parse(str_array[str_array.length-1]);


2022-09-30 18:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.