后台输出json:
var
text
=
eval
(
"("
+
")"
);
for(varkey in text)
3
{
4
varvalue = text[key];
5
}
20
// alert(innercode);
21
$
(
'select#cgt_id'
).
append
innercode
);
22
}
23
// alert(add_item_info);
24
25
26
xmlhttp
.
send
url
);
原文链接:https://www.f2er.com/ajax/165168.html
1
$arr= array('0'=>array('id'=>'11','title'=>112),'2'=>array('id'=>33,'title'=>44));
2
echo json_encode($arr);
2
我一般在后台返回的数据都转换成数组返回,好像也可以用Object返回,当然没有试过,其实我觉得XML能同样达到目的,不过json比较方便么。。一个字符串搞定
vartext = xmlhttp.responseText;
for(varkey in text)
3
4
5
还有一个对于上面的二维数组来说,用js遍历:
1
var
text
=
xmlhttp
.
responseText
;
2
text
eval
(
"("
+
text
+
")"
);
3
for
(
i
0
;
i
< text.length; i++){
4 var id = text[i].id;
5 title text[i].title;
6 }
2
3
4
5
6
然后贴上我一部分代码:
01
var
xmlhttp
;
02
if
(
window
.
XMLHttpRequest
)
03
new
();
04
}
else
05
ActiveXObject
"Microsoft.XMLHTTP"
06
}
07
url
"cgo_id="
encodeURIComponent
checkCgo
08
// alert(url);
09
open
"POST"
,
"additemresult.PHP"
true
10
setRequestHeader
"Content-type"
"application/x-www-form-urlencoded"
11
onreadystatechange
function
()
12
readyState
==
4
&&
status
200
13
add_item_info
responseText
14
cgt
15
// eval_r("var cgt = "+add_info);
16
17
for
key
in
18
Value
[
];
19
innercode
"
+
key
+
">"
Value
""
;
02
04
07
09
16
17
19
20
21
22
23
24