Ajax-为什么我会得到一个错误函数而不是成功?
我不明白为什么我的代码运行误差函数,而不是成功的。我不断从我的console.log得到这个Ajax-为什么我会得到一个错误函数而不是成功?
Object {readyState: 0, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
我想不出任何理由不让它执行我的逻辑,所以我试图把一个重定向在我的误差函数,这是我所得到的
Object {readyState: 0, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…}
基本上同样的事情发生,所以现在我没有真正的想法,我编辑windows to window
后我的问题是什么。 这是我在JS
function login(){
var username = document.getElementById("userID").value;
var password = document.getElementById("Password").value;
var postData = { "userID": username, "Password": password };
var postJSON = JSON.stringify(postData);
$.ajax({
url: "http://localhost:3000/api/login", // server url
type: "POST", //POST or GET
contentType: "application/json",
data: postJSON, // data to send in ajax format or querystring format
datatype: "JSON",
success: function(response) {
alert('success');
console.log(response);
window.location.replace("http://localhost/index.html");
},
error: function(response) {
alert('error');
console.log(response);
window.location.replace("http://localhost/index.html");
}
});
}
代码这是我的HTML代码。我正在使用onclick。
<input class="button" type="submit" id="submit" value="Log In" onclick="return login()"/>
那么究竟是什么地方出了问题我的代码?我想打电话给我的登录API:通过Ajax(本地主机3000/API /登录)与交那么这将检查MongoDB中正确输入和输出登录成功,然后重定向到另一个页面,如果输入的是正确的,并保持相同的页面,如果上通过输出“无效的登录ID或密码”输入错误。
UPDATE:
服务器端代码
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/myproject';
var authenticate = function(db, req, callback){
var cursor = db.collection('LoginID').find({"_id" : req.body.userID,
"Password" : req.body.Password
}).count(function(err,doc){
if(err) return callback(err);
if(doc == 0){
console.log('Invalid Login ID or Password');
return callback(null, doc);
} else {
console.log('Login Success');
return callback(null, doc);
}
});
}
module.exports = {
postCollection : function(req,res){
var username = req.body.userID;
var Password = req.body.Password;
//var createdDate = "<b>" + day + "/" + month + "/" + year + "</b>"
MongoClient.connect(url, function(err, db) {
//assert.equal(null, err);
if(err) {
res.send(err);
res.end();
}
authenticate(db, req, function(err,doc) {
if(err)
res.send(err);
else{
if(!doc){
res.send(' Invalid Login ID or Password ');
res.end();
} else {
res.send("Login success")
res.end();
}
}
db.close();
});
});
}
}
如果我没有记错,如果HTTP返回代码为2xx
成功回调只是调用。 如果您发回重定向,则将其视为错误。
文档提到它:
If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback.
从http://api.jquery.com/jquery.ajax/,的StatusCode部分。此外,您必须小心:如果AJAX请求收到302
响应,则不会执行重定向:这是您的Web浏览器的用户代理(经典导航),它会自动执行此操作,但对于XHR/AJAX,你必须实现它。
我不知道我理解你的问题:在标题中说,你不明白为什么错误回调被调用。你提到你在你的服务器上调用你的登录端点,并且如果凭证是好的,你想把用户重定向到一个页面。如果AJAX请求获得302响应,则调用错误回调是正常的。你记录你在参数中获得的对象,但是我没有看到这个对象有什么问题,你想得到什么? – sjahan
基本上我想能够获得成功的功能,以便我可以根据api给出的输出返回true或false,然后处理输入,然后根据true或false重定向,但是现在我不断收到错误我输入了什么。对不起,如果我不清楚 –
正如@farhadamjady所提到的,我想我们错过了你的代码服务器端:你的服务器发回了什么?什么是HTTP响应代码?响应代码将决定调用哪个回调。 – sjahan
你必须resplve这个'未捕获的ReferenceError:Windows不是defined'第一 –
这是window.location的。没有'。 =) – Repo
@Repo我重新编辑它,它给出同样的错误,所以我的问题依然存在。 –