我正在尝试从json webservice检索数据。
if (xmlHttp.status == 200 || xmlHttp.status == 0)
{
var result = xmlHttp.responseText;
json = eval("(" + result + ")");
}
我对var结果一无所获。当我用包含json对象的文本文件替换webservice时,我可以将json对象检索为responseText。请帮助
答案 0 :(得分:1)
首先要做的事情...... 永远不会永远使用eval
* 。 eval
=邪恶。
如何在AJAX中使用GET
...
try {
http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}
var url = "/uri/of/web-service?val1=Laura&val2=Linney" + Math.random();
var params = "val1=Laura&val2=Linney";
http.open("GET", url, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
// we have a response and this is where we do something with it
json = JSON.parse(http.responseText);
}
}
http.send();
如何在AJAX中使用POST
...
try {
http = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
http = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}
var url = "/uri/of/web-service";
var params = "val1=Laura&val2=Linney";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
// we have a response and this is where we do something with it
json = JSON.parse(http.responseText);
}
}
http.send(params);