0

I'm new to PHP, and i'm trying to fetch data from PHP to JavaScript. When i do that, JavaScript always giving me the same error "Unexpected token < in JSON at position 0". I don't have any idea what to do. I'm stuck...Help Help ! x)

There is the code

JavaScript

 var xmlhttp = new XMLHttpRequest();

 xmlhttp.onreadystatechange = function () {
 if (this.readyState == 4 && this.status == 200) {
    var myObj = JSON.parse(this.responseText);
    document.getElementById("demo").innerHTML = myObj.name;
  }
 };

xmlhttp.open("GET", "data.php", true);
xmlhttp.send();

PHP

<?php
  $myObj->name = "John";
  $myObj->age = 30;
  $myObj->city = "New York";

  $myJSON = json_encode($myObj);

  echo $myJSON;

?>

4
  • 1
    Please tag your questions accordingly. With your current tags, entire community reads this question (huge number from which, won't be competent in Javascript/PHP); rather, you could narrow down the prospective audience with the correct tags, this way you'll also attract more related audience. Commented Aug 30, 2020 at 12:10
  • 2
    Almost certainly the problem is that PHP is either throwing an error (and thus not returning JSON) or is also returning some HTML as well as the JSON. Notice it complains about < which if course is the first character of any HTML tag. Use your browser's network inspector to view the raw response coming back from the php script and check what the issue is. You need to ensure it always returns JSON alone, no other content or characters
    – ADyson
    Commented Aug 30, 2020 at 12:14
  • @ADyson Yes, i figured out that. But i can't find a solution for that problem. Is there any common reasons for that ?
    – Rakonjac
    Commented Aug 30, 2020 at 12:50
  • My comment already explains the likely reasons. Was there something specific you didn't understand? You need to go and check which one of them exactly is happening, in the way I suggested (by using the network inspector). If there's an error, you need to know what it is before you can fix it. If there's extra HTML being output, you need to know what it is so you can work out where in your code it's coming from.
    – ADyson
    Commented Aug 30, 2020 at 14:34

2 Answers 2

1

I think you are getting an issue of object
Object $myObj need to be initialized before using it
I have updated the code (Initialized the object) please use the below code on php page to resolve the issue

<?PHP
  $myObj = new StdClass();
  $myObj->name = "John";
  $myObj->age = 30;
  $myObj->city = "New York";

  $myJSON = json_encode($myObj);

  echo $myJSON; 
1
  • 1
    "an issue of object" isn't a clear explanation. I think I know what you're getting at, but at the moment your answer will be confusing for anyone who isn't familiar with the issue, even if the solution may be correct. Please edit your answer to be more specific and clear.
    – ADyson
    Commented Aug 30, 2020 at 14:36
0

Obviously, the response returned for your XMLHttpRequest is not a valid JSON. Provided that your PHP codesample is complete, PHP will most likely(*) echo an E_WARNING error before outputting the JSON string, although your code will work. Related question and solution here. Easiest way to check is to view the raw response in your browser's dev tools (supported by all major browsers). If the response contains anything except the JSON string, JSON.parse() will throw an error.

(*)This depends on your error reporting settings

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.