jQuery Ajax 跨域前端實現登入
封裝好的請求物件
(function() {
var ajax = {
ajax_cors: function(url, data, fn, dataType, contentType) {
$.ajaxSetup({
type: 'post',
dataType: dataType == null ? 'json' : dataType,
global: true,
contentType: contentType == null ? 'application / x - www - form - urlencoded' : contentType,
success: function(xhr, status, success) {
console.log('請求成功! ' + '當前狀態 ' + status)
console.log('響應體: ' + JSON.stringify(success))
fn(xhr, status, data)
},
error: function(xhr, status, error) {
console.log('請求失敗! ' + '當前狀態 ' + status)
console.log('響應體: ' + JSON.stringify(error))
fn(xhr, status, data)
},
xhrFields: {
withCredentials: true
},
crossDomain: false,
beforeSend: function(xhr) {
console.log('正在傳送請求: url => ' + url)
console.log('傳送資料型別: ' + this.contentType)
console.log('當前傳送資料: ' + this.data)
},
complete: function(xhr, status) {
console.log('請求已經完成')
console.log('響應型別: ' + this.dataType)
},
cache: false,
timeout: 3000,
})
$.ajax({
url: url,
data: data,
})
}
}
window.$kongjs = ajax;
})()
具體程式碼,跨域通過驗證碼登入
$(function() {
var imageUrl = 'http://127.0.0.1:8080/user?method=doYanCode';
var bool = true;
if (bool) {
$('#imageCode').attr('src', imageUrl);
}
$('#imageCode').click(function() {
bool = false
$kongjs.ajax_cors("http://127.0.0.1:8080/user?method=doYanCode", {}, function(data) {
$('#imageCode').attr("src", imageUrl)
console.log(data)
}, '*')
})
$('#submit').click(function() {
var formData = $("#loginForm").serialize();
$kongjs.ajax_cors("http://127.0.0.1:8080/user?"+formData,{'Access-Control-Ajax-Headers': 'cors'}, function(data) {
console.log(data)
if (data.code == 1) {
// 調整到首頁
location.href = "/web/index.html";
} else {
console.log(data.message);
}
}, '*','json')
})
})
相關文章
- jquery ajax 跨域請求jQuery跨域
- 淺析Ajax跨域原理及JQuery中的實現分析跨域jQuery
- 利用JQuery實現更簡單的Ajax跨域請求jQuery跨域
- JQuery Ajax跨域的問題jQuery跨域
- Ajax 跨域難題 - 原生 JS 和 jQuery 的實現對比跨域JSjQuery
- CORS方式實現ajax跨域 — nginx配置CORS跨域Nginx
- Ajax+SpringMVC實現跨域請求SpringMVC跨域
- ajax跨域post請求,如何實現呢跨域
- 跨域分散式系統單點登入的實現(CAS單點登入)跨域分散式
- 使用CORS實現ajax跨域簡單介紹CORS跨域
- jquery 之 jsonp 與 laravel 實現跨域jQueryJSONLaravel跨域
- Struts2+AJAX+JQuery 實現使用者登入與註冊功能。jQuery
- ajax實現的跨域請求程式碼例項跨域
- AJAX 跨域請求解跨域
- AJAX 跨域問題跨域
- 解決 ajax 跨域跨域
- Ajax跨域問題跨域
- ajax jsonp跨域JSON跨域
- 巧用javascript ajax,實現跨域請求外帶,增大漏洞危害JavaScript跨域
- PHP AJAX JSONP實現跨域請求使用例項PHPJSON跨域
- ajax 實現微信網頁授權登入網頁
- node+ajax+mysql實現登入註冊MySql
- 跨域請求之jQuery的ajax jsonp的使用解惑跨域jQueryJSON
- 重學前端(8)封裝ajax,http,跨域問題前端封裝HTTP跨域
- AJAX跨域完全講解跨域
- Ajax 跨域解決方案跨域
- 前端跨域前端跨域
- 實戰前端跨域問題前端跨域
- ajax實現跨域請求程式碼例項簡單介紹跨域
- KKB : Jquery實現Ajax請求jQuery
- JSP(ajax)+Servlet實現簡單的登入功能JSServlet
- layui使用html+servlet+ajax實現登入驗證UIHTMLServlet
- PHP結合Ajax實現登入驗證的DemoPHP
- ajax跨域的解決方案跨域
- 前端跨域整理前端跨域
- 利用Jsonp實現跨域請求,spring MVC+JQueryJSON跨域SpringMVCjQuery
- php實現SESSION跨域PHPSession跨域
- 解決 jquery使用ajax請求發生跨域問題的辦法jQuery跨域