我正在尝试通过XMLHttpRequest实现一个调用本地php文件的网站来获取json数据。正在从HTML表单调用登录功能。文件/var/www/picnit/api/member.api确实存在且具有足够的权限,并且已使用Chrome的Postman应用程序进行了测试。
//URL for member functions
var memberurl='/picnit/api/member.php';
//Request to be sent to the middleware
var request;
function login() {
//Get and validate user inputs
var username = $("input#Username").val();
var password = $("input#Password").val();
//Validation code omitted
//Getting here means that the inputs validated
//Request to be sent to the middleware
request = new XMLHttpRequest();
//Create function that handles response
request.onreadystatechange = function () {
//Make sure request is ready, we only care about when done
if(request.readyState === 4) {
//Good data, proceed to login
if(request.status === 200) {
alert('200');
}
//Invalid username/password combo
else if(request.status === 204) {
alert('204');
}
//Our request messed up
else if(request.status === 400) {
alert('400');
}
//Something else went wrong
else {
alert('Unknown error: ' + request.status);
}
}
};
//Open request
request.open('POST', memberurl, true);
//Send request
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var parms = 'username='+username+'&password='+password;
request.send(parms);
return false;
}
这始终会返回“未知错误:0”的警报,我无法弄清楚原因。我不认为它可以是跨域,因为它位于同一服务器(AWS EC2实例)上。而且我不完全确定相同的域策略是什么......
我一直在这里搜索其他帖子,但还没有找到答案。感谢您的高级帮助。