从jquery数组中删除开始/结束双引号

时间:2022-03-12 05:45:00


I am getting data from php with this jquery:

我从这个jquery获取来自php的数据:

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?

来自php的数据如下所示:“2014-02-28”,“2014-03-01”,“2014-03-02”console.log返回:[“”2014-02-28“,”2014-03 -01“,”2014-03-02“”]和jquery不起作用但是当我手动输入日期而不是保留时,它就起作用了。像这样:var res = new Array(“2014-02-28”,“2014-03-01”,“2014-03-02”);和console.log [“2014-02-28”,“2014-03-01”,“2014-03-02”]所以我看到的问题是在数组的开头和结尾的那些引号中。他们可以被删除吗?

3 个解决方案

#1


0  

Use JSON.parse

使用JSON.parse

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

Fiddle Demo

小提琴演示

#2


1  

try using

尝试使用

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

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

编辑:您不需要创建数组。

#3


0  

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

你需要在php中对结果进行编码,如下所示:

test.php file

test.php文件

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

and your ajax:

和你的ajax:

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

#1


0  

Use JSON.parse

使用JSON.parse

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

Fiddle Demo

小提琴演示

#2


1  

try using

尝试使用

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

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

编辑:您不需要创建数组。

#3


0  

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

你需要在php中对结果进行编码,如下所示:

test.php file

test.php文件

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

and your ajax:

和你的ajax:

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