2017-04-23 78 views
0

我有一个带有Ajax请求的脚本。 我从一个php变量中获得以下数组。 该数组在脚本从php变量转换javascript数组

prova.php

<?php $x='{x:"0.7129415111913572", y:"0.4862778730703259", note:"a", type:"2", id:"2"},{x:"0.27190696187655316", y:"0.7572898799313894", note:"b", type:"2", id:"3"}'; ?> 
<textarea name="update2" id="update2"><?php echo htmlspecialchars($x); ?></textarea> 

的index.php

$.ajax({  
    type: "GET", 
    url: "/admin/include/data/prova.php?id="+datastr_data, 
    data: datastr_data, 
    cache: false, 
    success: function(html){ 
     $("#data-result_map").html(html); 
 var notes2=[$('#update2').val()]; // NOT WORKS (alert produces a result of the string) 
     var notes2=[<?php echo $xxxx ?>]; //WORKS (alert produces [object, Object],[object, Object] 

     $img.imgNotes("import",notes2);       

    },async: false 

}); 

通过一个内部变量作品创建和相同的内容被转换成用于对象,而从外面的步骤仍然是一个变量,不起作用。

+1

为什么不简单地从你的php端返回一个json数据然后将它附加到你的html textarea? – hassan

回答

0

从PHP返回一个json对象。

echo json_encode($result); 

您可以使用JSON.parse来操纵的结果在您的JS 成功功能。

var result = JSON.parse(html); 
+0

非常感谢100%的工作 –