2017-04-21 76 views
2

enter image description here我有以下JSON數據:無法執行AJAX術後

[ 
    { 
    "Password": "tedd", 
    "Username": "john", 
    "status": true 
    } 
] 

其中我想用post方法

<label for="Username">Username:</label> 
<input type="text" id="txtUserName" placeholder="Enter UserName"/> 
<label for="Password">Password</label> 
<input type="text" id="txtPass" placeholder="Enter Password" /> 
<input type="button" onclick="loginc()" value="Click Me" /> 

應用邏輯以消耗:

function loginc() { 
var username = document.getElementById("txtUserName").value; 
if (username == "") { 
    alert("Please enter the valid name"); 
    return false; 
} 

var password = document.getElementById("txtPass").value; 
if (password == "") { 
    alert("Please enter valid password"); 
} 
var urlink = "http://192.168.0.112/Service1.svc/getlogin"; 

var datat = { Username: "username", Password: "password" }; 



$.ajax({ 
    type: 'POST', 
    url: urlink, 
    dataType: "json", 
    contentType: "application/x-www-form-urlencoded; charset=UTF-8", 

    data:datat, 

    success: function (resp) {     
     alert(resp); 
     if (resp.length != 0) { 
      alert("Logged On"); 
     } else { 
      alert("Wrong Credentials"); 
     } 

    }, 
    error: function (e) { 

     alert("Invalid"); 

    } 
}); 

所以這裏的問題是,當我輸入用戶名和密碼時,點擊按鈕而不是驗證我是n沒有得到任何迴應,並且在錯誤中顯示錯誤的憑據。

This is the result im getting in the post man

+4

如果你得到''錯誤的憑證'',你會得到一個響應,這只是空的,這意味着錯誤最有可能是服務器端? – adeneo

+0

@Madpop用'alert(e.responseText)'替換'alert(「Invalid」)'並檢查確切的錯誤,然後你會知道你做錯了什麼。 – Krish

+0

@克里希實際上取代了你說的話,我沒有得到任何迴應文本 – Madpop

回答

2

一個解決方案是將參數追加到url

var datat = { Username: username, Password: password }; 

$.ajax({ 
    type: 'POST', 
    url: urlink+'?username='+username+'&password='+password, 
    dataType: "json", 
    contentType: "application/x-www-form-urlencoded; charset=UTF-8", 
    data:datat, 
    success: function (resp) {     
     alert(resp); 
     if (resp.length != 0) { 
     alert("Logged On"); 
     } else { 
     alert("Wrong Credentials"); 
     } 

    }, 
    error: function (e) { 
     alert("Invalid"); 
    } 
}); 

另一種解決方法是使用JSON.stringify()方法發送對象。

contentType是數據要發送的類型,所以你需要application/json;默認爲application/x-www-form-urlencoded; charset=UTF-8.

如果使用application/json,你必須按順序發送JSON對象使用JSON.stringify()

JSON.stringify()將一個javascript對象轉換爲json文本並將其存儲在一個字符串中。

$.ajax({ 
    type: 'POST', 
    url: urlink, 
    dataType: "json", 
    contentType: "application/json", 
    data:JSON.stringify(datat), 
    success: function (resp) {     
     alert(resp); 
     if (resp.length != 0) { 
     alert("Logged On"); 
     } else { 
     alert("Wrong Credentials"); 
     } 

    }, 
    error: function (e) { 
     alert("Invalid"); 
    } 
}); 
+0

沒有沒有結果像圖像相同 – Madpop

+0

我認爲錯誤是從服務器端,因爲你收到空響應。 –

+0

@ Alexandru-Ionut Mihai請檢查帖子的人的形象 – Madpop

3
var datat = { Username: "username", Password: "password" }; 

在這裏,你要發送的文字串usernamepassword,而不是變量。刪除引用:

var datat = { Username: username, Password: password }; 
+1

_ * Facedesk * _。我想我們最好把它當作「Typo」。 – Cerbrus

+0

沒有相同的沒有得到任何迴應,請檢查我的帖子的圖像 – Madpop