Jakob
Jakob

Reputation: 3546

Remove start/end double quotes from jquery array


I am getting data from php with this jquery:

var reserved=null;
$.ajax({
        url: 'test.php',
        type: 'GET',
        dataType: 'html',
        async: false,
        success: function(data) {
            reserved=data;
        } 
     });

var res = new Array(reserved);
console.log(res);

Data from php looks like this: "2014-02-28", "2014-03-01", "2014-03-02"
console.log returns this: [""2014-02-28", "2014-03-01", "2014-03-02""] and jquery doesnt work
But when I enter dates manually instead of reserved then it works.
Like this: var res = new Array("2014-02-28", "2014-03-01", "2014-03-02");
And console.log ["2014-02-28", "2014-03-01", "2014-03-02"]
So problem as I see it is in those quotes at start and end of array. Can they be removed?

Upvotes: 1

Views: 3932

Answers (3)

Adesh Pandey
Adesh Pandey

Reputation: 769

try using

var res = $.parseJSON(reserved);
console.log(res);

EDIT: you don't need to create an array.

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115222

Use JSON.parse

var res = JSON.parse('['+reserved+']');

Fiddle Demo

Upvotes: 1

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

you need to encode the result inside your php like this:

test.php file

//your code
echo(json_encode($your_array);

and your ajax:

$.ajax({
        url: 'test.php',
        type: 'GET',
        dataType: 'json',
        async: false,
        success: function(data) {
            reserved=data;
        } 
     });

Upvotes: 0

Related Questions