2017-06-13 72 views
1

我不明白爲什麼我的代碼運行誤差函數,而不是成功的。我不斷從我的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(); 
     }); 
    }); 
} 
} 
+0

你必須resplve這個'未捕獲的ReferenceError:Windows不是defined'第一 –

+0

這是window.location的。沒有'。 =) – Repo

+0

@Repo我重新編輯它,它給出同樣的錯誤,所以我的問題依然存在。 –

回答

1

如果我沒有記錯,如果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,你必須實現它。

+0

我不知道我理解你的問題:在標題中說,你不明白爲什麼錯誤回調被調用。你提到你在你的服務器上調用你的登錄端點,並且如果憑證是好的,你想把用戶重定向到一個頁面。如果AJAX請求獲得302響應,則調用錯誤回調是正常的。你記錄你在參數中獲得的對象,但是我沒有看到這個對象有什麼問題,你想得到什麼? – sjahan

+0

基本上我想能夠獲得成功的功能,以便我可以根據api給出的輸出返回true或false,然後處理輸入,然後根據true或false重定向,但是現在我不斷收到錯誤我輸入了什麼。對不起,如果我不清楚 –

+0

正如@farhadamjady所提到的,我想我們錯過了你的代碼服務器端:你的服務器發回了什麼?什麼是HTTP響應代碼?響應代碼將決定調用哪個回調。 – sjahan

相關問題