简体   繁体   English

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

[英]Remove start/end double quotes from jquery array


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" 来自php的数据如下所示: "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 console.log返回以下内容: [""2014-02-28", "2014-03-01", "2014-03-02""]和jquery不起作用
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"); 像这样: 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"] 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? 可以将它们删除吗?

try using 尝试使用

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

EDIT: you don't need to create an array. 编辑:您不需要创建一个数组。

Use JSON.parse 使用JSON.parse

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

Fiddle Demo 小提琴演示

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;
        } 
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM