0

My php file ends with:

echo json_encode($array1);
echo ";";
echo json_encode($array2);

and prints out e.g.

[1358499135965,68];[1358499140000,2]

My javascript code looks so:

function requestData() {
    $.ajax({
        url: 'livedata.php',
        success: function(point) {    
            var yenidata = point.split(";");
            alert(yenidata[0]);
            alert(yenidata[1]);
        });
    }

Why do I not get an alert?

6
  • 4
    Do you get any error on the console? Commented Jan 18, 2013 at 9:02
  • 1
    if your entire response is [1358499135965,68];[1358499140000,2], then it is invalid JSON and may not be getting passed into the success handler. Commented Jan 18, 2013 at 9:04
  • oh yeah ... "Uncaught ReferenceError: $ is not defined " Commented Jan 18, 2013 at 9:04
  • You are missing a closing brace at the end. Commented Jan 18, 2013 at 9:04
  • You havent included jQuery library I guess Commented Jan 18, 2013 at 9:07

2 Answers 2

2

Your JSON is not valid.

Try:

echo '[';
echo json_encode($array1);
echo ",";
echo json_encode($array2);
echo ']';

Now the PHP page will print you: [[1358499135965,68],[1358499140000,2]] Which can be parsed automatically as JSON using dataType:"json" in your ajax call.

When you include jQuery your code should look like:

function requestData() {
   $.ajax({
      url: 'livedata.php',
      dataType: 'json',
      success: function(point) {
         console.log(point[0]); //Array [1358499135965,68]
         console.log(point[1]); //Array [1358499140000,2]
      }
   });
}
Sign up to request clarification or add additional context in comments.

2 Comments

if he adds dataType:"json" to the ajax func, he doesn't even need to parse as json
But i have to echo ",;"; remove the ; ... echo ","; Now it works! Thanks
2

Uncaught ReferenceError: $ is not defined means you're not including jQuery. You need to have that to use the features you're trying to use.

3 Comments

This php file and javascript is included in a bigger file. So jQuery library is included
Is it included before that JS?
Try jQuery.ajax instead of $.ajax

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.