0

I'm using something like this,

jQuery(function($) {
    function updateData()
    {

        var html = $.ajax({
            url: 'ajaxMap.php?id='+lastId,
            success: function(data) {
                            alert(data);
            }
        });
    }

    setInterval(function(){
        updateData()
    }, 2000);
});

I expect to get a JavaScript array after querying the PHP file. As shown in the code, 'data' is returned after querying the url (The PHP file simply outputs a JavaScript array). When I use alert(data), it shows me the same array. But I cannot use this as an array. For example, I can't use array functions like .length, etc on this. JavaScript treats this as a simple string. What am I doing wrong here? Let me know if I need to provide more information.

2 Answers 2

0

Use this JS (not tested)

$(document).load(function(){
  setInterval(function() {
    $.getJSON('ajaxMap.php?id='+lastId,function(data) {
      alert(data);
    });
  });
});

And json_encode($array) on server side ('application/json' MIME header would be great but it is not needed)

Sign up to request clarification or add additional context in comments.

Comments

0

You need to parse Json string like this or this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.