试图用php文件填充jquery?

时间:2013-11-23 18:25:56

标签: javascript jquery json

我想尝试一些类似这样的javascript

var locations = [
     ['Title A', 3.180967,101.715546, 1],
     ['Title B', 3.200848,101.616669, 2],
     ['Title C', 3.147372,101.597443, 3],
     ['Title D', 3.19125,101.710052, 4]
];

所有位置数据都是由.php文件动态创建的。我用ajax这样调用php文件

$.ajax({
        type:"POST",
        //dataType: "json",
        url:"/search.php",
        data: {posLat: lat, posLng: lng},
        success:function(returned_data){

        var jsonRes = JSON.parse(returned_data);
        console.log("JSON "+jsonRes[0].routelength)

        $.each(jsonRes , function( index, obj ) {
            var tempArr = [];
            $.each(obj, function( key, value ) {
            console.log(value);
            tempArr.push(value);
            });
            locations.push(tempArr);
            });

          console.log("locations" +locations);

          }
        })

但现在所有的位置数据只是一个大字符串而不是[...],[...],[...]

returned_data看起来像

[ {
    "routeTitle":"Route 1",
    "routelength":9.15,
    "startPointLat":"52.16259353572091",
    "startPointLon":"4.500060081481934"
  },

  {

    "routeTitle":"station",
    "routelength":3.96,
    "startPointLat":"52.16537734384885",
    "startPointLon":"4.483022689819336"
   }, 
  { 
     "routeTitle":"small",
     "routelength":3.15,
     "startPointLat":"52.154267396622025",
     "startPointLon":"4.486691951751709"
  }
] 

编辑ajax部分,我能够获得[[...],[...],[...]格式,但它似乎没有解析/转换为数组

success:function(returned_data){
                    var routes_data=returned_data;
                    var jsonRes = JSON.parse(returned_data);
                    $.each(jsonRes, function(index, val) {
                        var tempArr = [];
                        //routes2.push(jsonRes[index].val);
                        tempArr.push("'"+val.routeTitle+"'");
                        tempArr.push(val.startPointLat);
                        tempArr.push(val.startPointLon);
                        tempArr.push(999);                      
                        tempArr.push("'"+val.routelength+"'");
                        locations2.push("["+tempArr+"]");
                     });
                    locations = "["+locations2+"]";
                    //locations =[['Test title',52.154267396622025,4.486691951751709,999,'3.15'],['Route 1',52.16259353572091,4.500060081481934,999,'9.15'],['station',52.16537734384885,4.483022689819336,999,'3.96']];
                    console.log("locations " +locations);
                    createMarkerV3(map,locations);  
                     }

3 个答案:

答案 0 :(得分:2)

尝试将header('Content-type: application/json');放入php。

然后浏览器会期望原始json数据并自动解析,因此您不需要 javascript中的JSON.parse

答案 1 :(得分:0)

我在这里拍了很长时间,想想也许这个?我不知道我是否完全理解。

<script>
var locations = <?php echo json_encode($phpData); ?>;
</script>

答案 2 :(得分:0)

这应该满足您的需求,

var xSrc = [{....}]; //DATA given by you

var xTarget = [];
var xTemp = [];

$.each(xSrc,function(Index,ArrayVal){
    $.each(ArrayVal,function(key,val){
        xTemp.push(val);       
    });

    xTarget.push(xTemp);
    xTemp = [];
});

alert(JSON.stringify(xTarget));

DEMO