1. 程式人生 > >服務端返回json資料,前端通過判斷json資料值做跳轉頁面

服務端返回json資料,前端通過判斷json資料值做跳轉頁面

<script
src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>


$(document).ready(function() {
$("#tijiao").click(function() {
alert("要執行ajxs")
/*    var fd = new FormData(document.querySelector("form")); */
$.ajax({
type : "POST",
url : 'user/login.do',

/*      contentType : "application/json; charset=utf-8", *

contentType : "application/x-www-form-urlencoded",

data : $('#J-normal-form').serialize(),
/*   data:{"username":"shn","password":"123"},   */
/*  data: fd,  */

dataType : "json",

/*data代表json整個資料物件*/

/*success是ajax成功的訪問controller(後端)之後執行的方法,與controll裡面的返回值無關*/

success : function(data) {
console.log("123212454");


console.log(data.status);
if (data.status == 0) {
console.log("等於0哦");
}
console.log(data.data);

console.log(data.msg);

/*頁面跳轉*/

window.location.href = "index.html"

},

/*ajax沒有成功執行後臺程式碼執行的函式*/

error : function() {
alert("提交資料失敗!");
/*   location.href="index.html" */
}
});


});


})
</script>