1. jQuery中ajax配置项中的使用type与method的区别
本质上两个配置项是没有区别的,区别在于两者出现的时间不同,type对于目前jQuery的版本全部兼容,也就是说
1 $.ajax({ 2 type: 'POST', 3 url: '/login', 4 dataType: 'json',若出现跨域请求请使用'jsonp' 5 data: { 6 'userName': userName, 7 'password': password 8 }, 9 success: function (e) { 10 if (e.code == 200) { 11 //这里登录成功后进行页面跳转 12 window.location.href = '/index'; 13 alert(e.message); 14 } else { 15 alert(e.message); 16 } 17 }, 18 error: function () { 19 alert("Error!"); 20 } 21 });
这种方式书写,type类型是GET就是GET请求,是POST请求就是POST请求。
2. 在jQuery1.9以后的版本中使用ajax时,可以使用如下方式
1 $.ajax({ 2 method: 'POST',这里不同了 3 url: '/login', 4 data: { 5 'userName': userName, 6 'password': password 7 }, 8 success: function (e) { 9 if (e.code == 200) { 10 window.location.href = '/index'; 11 alert(e.message); 12 } else { 13 alert(e.message); 14 } 15 }, 16 error: function () { 17 alert("Error!"); 18 } 19 });