1

I have successfully retrieved a response from a PHP file into Javascript, however it is returned as one long string; when I need to put each separate element into a separate place in the HTML.

  $.post("includes/skill_scout_1.php", {
      selected_player: selected_transfer_player
      }, function (data, status) {
        $("#transfers-scout-1").html(data);
      });

I am looking to do something like this

  $.post("includes/skill_scout_1.php", {
      selected_player: selected_transfer_player
      }, function (data, status) {
          var obj = JSON.parse(data);
          $("#transfers-scout-1").html(obj.scout1);
          $("#transfers-scout-2").html(obj.scout2);
          $("#transfers-scout-3").html(obj.scout3);
      }

However it returns no data and I get the following error in the web console SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

Within the PHP file I have this to return the data to Javascript as JSON:

$myObj->scout1 = $scout1;
$myObj->scout2 = $scout2;
$myObj->scout3 = $scout3; 
$myJSON = json_encode($myObj);
echo $myJSON;

Any help would be greatly appreciated.

When I try the below #transfer-scout-4 fills with the entire string (as expected), but #transfer-scout-1, #transfer-scout-2 and #transfer-scout-3 are blank.

      $.post("includes/skill_scout_1.php", {
  selected_player: selected_transfer_player
  }, function (data, status) {
      $("#transfers-scout-4").html(data);
      var obj = JSON.parse(data);
      $("#transfers-scout-1").html(obj.scout1);
      $("#transfers-scout-2").html(obj.scout2);
      $("#transfers-scout-3").html(obj.scout3);
  
  });

Thanks

BenSellars
  • 79
  • 9
  • Java != JavaScript. Make sure you understand the difference, and then update you question accordingly. – ADyson Jun 27 '20 at 08:34
  • Anyway it's unclear what the issue is. The code looks like it should probably work. Please explain any debugging you have done which would help us understand precisely what is happening with the data. – ADyson Jun 27 '20 at 08:39
  • Thanks for the tips. Its encouraging that you think the code should work. I'll try some more debugging and post the errors if it still doesn't work. – BenSellars Jun 27 '20 at 08:44
  • The problem has to be in this part: var obj = JSON.parse(data); Do I need to add a line in the header or something to get the parse to work? – BenSellars Jun 27 '20 at 10:05
  • anything after JSON.parse(data) is ignored by the code, even if it is just $("#transfers-scout-2").html(data); nothing is returned. – BenSellars Jun 27 '20 at 10:31
  • Have you checked your browser's Console for errors? Have you logged the contents of `data` to inspect it? – ADyson Jun 27 '20 at 10:36
  • contents of data look fine to me {"position":"Striker","scout1":"is sharp as a razor blade","scout2":"watch out, he'll lose your defenders","scout3":"is good in the air", etc..."scout16":"provides that creative spark"} – BenSellars Jun 27 '20 at 11:17
  • but i get this error Warning: Creating default object from empty value in C:\xamppone\htdocs\mezzala\includes\skill_scout_1.php on line 600. – BenSellars Jun 27 '20 at 11:18
  • line 600 is this $myObj->position = $detailed_position; – BenSellars Jun 27 '20 at 11:18
  • there are no error in the console - but thanks for pointing out - something I should start using! – BenSellars Jun 27 '20 at 11:33
  • This article: https://stackoverflow.com/a/8900730/5947043 will explain about the "Creating default object from empty value" error. But that isn't affecting your Javascript code really. – ADyson Jun 28 '20 at 07:36
  • Assuming your HTML is correct then there's nothing obviously wrong with the code. Try without the JSON.parse, in case jquery has already parsed the data for you (it might, if it detects that it's JSON - although that's likely to then result in an error when you try to parse it, but you said there were no errors). Demo: http://jsfiddle.net/1gtk8zj5/1/ – ADyson Jun 28 '20 at 07:54
  • Thanks for you help. It is much appreciated. – BenSellars Jun 28 '20 at 13:11
  • Without the JSON.parse, but with html(data.scout1) I get nothing returned. – BenSellars Jun 28 '20 at 19:06
  • I am now getting this error in the web console SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data – BenSellars Jun 28 '20 at 20:25
  • what _is_ the unexpected character? It usually tells you? And what does your code look like now, so I can be sure? – ADyson Jun 29 '20 at 06:42
  • this is the message in the web console: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data2 mezzala:531:26 http://localhost/mezzala/:531 jQuery 4 fire fireWith done callback – BenSellars Jun 29 '20 at 12:10
  • I can't see where it tells me which character is the problem. – BenSellars Jun 29 '20 at 12:11
  • Current code (trying different things to see what happens): $.post("includes/skill_scout_1.php", { selected_player: selected_transfer_player }, function (data, status) { $("#transfers-scout-4").html(data); $("#transfers-scout-2").html(data.scout2); $("#transfers-scout-1").html(data.scout1); var obj = JSON.parse(data); $("#detailed-position").html(obj.detailed_position); $("#transfers-scout-3").html(data.scout3); }); – BenSellars Jun 29 '20 at 12:14
  • $myObj->scout1 = $scout1; $myObj->scout2 = $scout2; $myObj->scout3 = $scout3; $myObj->scout4 = $scout4; $myObj->scout5 = $scout5; $myObj->scout6 = $scout6; $myObj->scout7 = $scout7; $myObj->scout8 = $scout8; $myObj->scout9 = $scout9; $myObj->scout10 = $scout10; $myObj->scout11 = $scout11; $myObj->scout12 = $scout12; $myObj->scout13 = $scout13; $myObj->scout14 = $scout14; $myObj->scout15 = $scout15; $myObj->scout16 = $scout16; $myJSON = json_encode($myObj); echo $myJSON; – BenSellars Jun 29 '20 at 12:16
  • _“I can't see where it tells me which character is the problem.”_ - open your browser dev tools, go to the network panel, inspect the request. Check what the actual response was. Most likely it will start with something like `` or ``, because your PHP script contained a fatal error, so that your web server returned the error document for a 500 Internal Server Error instead. And anything starting with those kind of characters, is obviously not valid JSON. – CBroe Jul 08 '20 at 12:38
  • Cheers, never used that network tab before. Looks like it will come in useful. – BenSellars Jul 08 '20 at 19:18

0 Answers0