I've been trying receive information with AJAX,JS,JSON and PHP. The context is the following:
我一直在尝试使用AJAX、JS、JSON和PHP来获取信息。内容如下:
I have a JS file, i sending from this file an id, that it receive a PHP file and this file makes some queries, of which the data is stored in an object and sent to a JS file to update some attributes of HTML. I can send the id from JS to PHP correctly. Then, i've checked that i can pass the information from the queries information to an PHP object correctly too.
我有一个JS文件,我从这个文件发送一个id,它接收一个PHP文件,这个文件进行一些查询,其中的数据存储在一个对象中,然后发送给一个JS文件,以更新HTML的一些属性。我可以正确地将id从JS发送到PHP。然后,我检查了是否可以将查询信息中的信息正确地传递给PHP对象。
Here's the problem...when i receive the data in the JS file and this information pass to the eval() function, it give me an error.
这正是问题的所在……当我接收JS文件中的数据并将此信息传递给eval()函数时,它会给我一个错误。
Notes:
注:
-
When I take out the eval function, I can't access the attributes, because it declares as "undefined".
当我取出eval函数时,我不能访问属性,因为它声明为“undefined”。
-
It's strange, but when I print the argument "datos" (with alert ()) displays all the information requested correctly.
这很奇怪,但是当我打印参数“datos”时(使用alert())显示所有请求的正确信息。
-
I don't speak english very well, excuse me my syntactic and semantic errors.
我英语说得不好,请原谅我的句法和语义错误。
infoAction.php
infoAction.php
<?php
include 'sitioTuristico.php';
$id = $_GET['id'];
$sitio = new sitioTuristico("SI01");
if($sitio->getID_SI()=="INEX"){
print "The place doesn't exist";
exit;
}
$response = new stdClass();
$response->ID_SI = $sitio->getID_SI();
$response->URL = $sitio->getURLS();
$response->Nombre = $sitio->getNombre();
$response->Descripcion = $sitio->getDescripcion();
$response->Promedio_nota = $sitio->getPromedio_Nota();
$response->Nombre_Cat = $sitio->getNombre_Cat();
$response->Nombre_Ciu = $sitio->getNombre_Ciu();
/* i tried this way too
$arr = array('ID_SI' => $sitio->getID_SI(),
'URL' => $sitio->getURLS(),
'Nombre'=> $sitio->getNombre(),
'Descripcion'=> $sitio->getDescripcion(),
'Promedio_nota'=> $sitio->getPromedio_Nota(),
'Nombre_Cat' => $sitio->getNombre_Cat(),
'Nombre_Ciu' => $sitio->getNombre_Ciu());
*/
echo json_encode($response);
?>
info-turistica-info.js
info-turistica-info.js
$(document).ready(function(){
$.ajax({
type: "GET",
url: "php/indexAction.php",
data: { id:loadData() },
async: true,
success: function(datos){
var dataJson = eval(datos); **//here throws an error**
alert(datos); **//It print all the information correctly**
alert(datos.ID_SI); **//It print "undefined"**
$('img[name=imagenPrincipal]').attr("src",dataJson[i].URL[0]);
$('img[name=imagenPrincipal]').attr("id",dataJson[i].ID_SI);
},
2 个解决方案
#1
1
You are using dataJson[i].URL[0] and you don't even have "i" defined anywhere. Also don't ever use eval because eval === evil according to Douglas Crockford. Use JSON.parse(datos) and then do this:
您正在使用dataJson[我]。URL[0],你甚至没有在任何地方定义“i”。也不要使用eval,因为根据Douglas Crockford的说法,eval === = evil。使用JSON.parse(datos),然后:
$('img[name=imagenPrincipal]').prop("src",dataJson.URL);
$('img[name=imagenPrincipal]').prop("id",dataJson.ID_SI);
Hope it helps!
希望它可以帮助!
#2
1
First, use JSON.parse(datos);
instead of eval
. Secondly, You need to use something like this:
首先,使用JSON.parse(输出数据);而不是eval。其次,你需要使用如下内容:
$('img[name=imagenPrincipal]').prop("src",dataJson.URL);
$('img[name=imagenPrincipal]').prop("id",dataJson.ID_SI);
#1
1
You are using dataJson[i].URL[0] and you don't even have "i" defined anywhere. Also don't ever use eval because eval === evil according to Douglas Crockford. Use JSON.parse(datos) and then do this:
您正在使用dataJson[我]。URL[0],你甚至没有在任何地方定义“i”。也不要使用eval,因为根据Douglas Crockford的说法,eval === = evil。使用JSON.parse(datos),然后:
$('img[name=imagenPrincipal]').prop("src",dataJson.URL);
$('img[name=imagenPrincipal]').prop("id",dataJson.ID_SI);
Hope it helps!
希望它可以帮助!
#2
1
First, use JSON.parse(datos);
instead of eval
. Secondly, You need to use something like this:
首先,使用JSON.parse(输出数据);而不是eval。其次,你需要使用如下内容:
$('img[name=imagenPrincipal]').prop("src",dataJson.URL);
$('img[name=imagenPrincipal]').prop("id",dataJson.ID_SI);