我想将带有用户名和密码的 lonin 请求发送到具有公共 ULR(不在本地主机中)的 Funambol 服务器。我已经有了 ID 和密码,而且我可以在服务器中使用我的 ID 登录,但是当我想使用 JavaScript 发送登录请求时它没有响应我任何东西,我的问题是:有什么特殊的方式来发送请求并接收服务器返回的数据?我尝试了下面的代码,但没有得到任何结果(我得到的状态结果:http.readyState = 4 和 http.status = 0)。如果有人知道这一点,请建议如何发送请求并从响应中检索数据?
var http = false;
var user = "myid";
var pass = "mypassword";
if (navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
function handle() {
if (http.readyState == 4) {
var response = http.responseText;
//var response = eval('(' + http.responseText + ')');
//+"--"+ http.readyState +"--"+http.status;
document.getElementById('p').innerHTML = "response: " + response.data;
//countcontacts();
}
}
function login() {
http.onreadystatechange = handle;
http.open("POST", "/123.125.221.15/login?action=login&login="+user
+"&password="+pass, true);
http.send(null);
}
function countcontacts() {
http.onreadystatechange = function() {
if (http.readyState == 4 && http.status == 200) {
the_object = eval('(' + http.responseText + ')');
document.getElementById('p').innerHTML = "Found "
+ the_object.count + " contacts.";
}
}
http.open("POST", "/123.125.221.15/contact?action=count", true);
http.send(null);
}
还有为什么var response = eval('(' + http.responseText + ')');
会抛出这个异常:
uncaught syntaxerror unexpected token ) at file android_asset www index html
提前致谢!!