我最近刚开始使用ajax,我遇到了一个小问题 . 代码如下:
-
输入表单 - >值到AJAX .
-
AJAX将表单值发送到将函数发送到服务器的php函数 .
-
服务器返回带有查询结果的stdClass对象 .
-
PHP将结果作为responseText发送回AJAX,以显示在webapp上 .
The problem:
stdClass对象是一个PHP对象,我现在坚持,不可能更改有问题的服务器的输出格式 .
我已经尝试了所有可能的数组/对象/变量 - 我能想到的组合,并尝试json_encode对象 . 但是,此解决方案仅返回一组很长的字符串,对象上的var_dump也是如此 . 我可能是一个可怕的错误,完全没有与json的经验 .
我需要一些关于如何以我可以用JavaScript输出的格式将对象传递给AJAX的指导 .
AJAX Code:
function checkSerial(serial) {
console.log(serial);
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
$responseText=xmlhttp.responseText;
console.log($responseText);
}
}
xmlhttp.open("GET","../module/getdevice.php?serial="+serial, true);
xmlhttp.send();
}
Outputted stdClass. Sorry about the censored values.
object(stdClass)#5 (15) {
["censored"]=>
string(12) "censored"
["censored"]=>
string(29) "censored"
["censored"]=>
string(1) "censored"
["censored"]=>
string(8) "censored"
["censored"]=>
string(7) "censored"
["censored"]=>
string(0) "censored"
["censored"]=>
string(56) "censored"
["censored"]=>
string(47) "censored"
["censored"]=>
string(47) "censored"
["censored"]=>
string(30) "censored"
["censored"]=>
string(14) "censored"
["censored"]=>
string(0) "censored"
["censored"]=>
string(0) "censored"
["censored"]=>
string(0) "censored"
["censored"]=>
希望这篇文章不重复,我已尽力积极寻找解决方案 . 先感谢您 !
1 回答
您已经有了解决方案:服务器端
json_encode()
.要将javascript中的长字符串转换为可以使用的对象,需要解析它: