Skip to content

Instantly share code, notes, and snippets.

@GithubMrxia
Last active October 30, 2020 12:44
Show Gist options
  • Save GithubMrxia/3864ff2ad5578e8876264de5b1a5528d to your computer and use it in GitHub Desktop.
Save GithubMrxia/3864ff2ad5578e8876264de5b1a5528d to your computer and use it in GitHub Desktop.
$(function () {
// ajax 全局设置
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
// 'Content-type': 'multipart/form-data',
// "Content-type": "multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2)),
},
beforeSend: function () {
$('[type="submit"]').attr('disabled', true);
layer.load();
},
complete: function () {
$('[type="submit"]').removeAttr('disabled');
layer.closeAll('loading');
}
});
// normal
$.ajax({
type: "post",
url: "url",
dataType: "json",
cache:false,
data: "data",
success: function (response) {
}
});
// file
// ①
// cache: 缓存
// 当发起一次请求后, 会把获得的结果以缓存的形式进行存储, 当再次发起请求时, 如果 cache 的值是 true, 那么会直接从缓存中读取, 而不是再次发起一个请求了。
// 从 cache 的工作原理可以得出, cache 的作用一般只在 get 请求中使用。
// ② processData: 处理数据
// 默认情况下, processData 的值是 true, 其代表以对象的形式上传的数据都会被转换为字符串的形式上传。 而当上传文件的时候, 则不需要把其转换为字符串, 因此要改成false
// ③ contentType: 发送数据的格式
// 和 contentType 有个类似的属性是 dataType, 代表的是期望从后端收到的数据的格式, 一般会有 json、 text…… 等
// 而 contentType 则是与 dataType 相对应的, 其代表的是 前端发送数据的格式
// 默认值: application / x - www - form - urlencoded
// 代表的是 ajax 的 data 是以字符串的形式 如 id = 2019 & password = 123456
// 使用这种传数据的格式, 无法传输复杂的数据, 比如多维数组、 文件等
// 有时候要注意, 自己所传输的数据格式和ajax的contentType格式是否一致, 如果不一致就要想办法对数据进行转换
// 把contentType 改成 false 就会改掉之前默认的数据格式, 在上传文件时就不会报错了。
$.ajax({
type: "post",
url: "url",
dataType: "json",
cache: false,
processData: false,
contentType: false,
data: "data",
success: function (response) {
}
});
$("#submit").click(function(e) {
e.preventDefault();
var data = $("#login").serialize();
$.ajax({
type : "post",
url : '{{route(' user.login ')}}',
dataType : "json",
cache : false,
data : data,
success : function(response) {
console.log(response);
}
});
})
});
# 刷新当前页面
location.reload();
# 取代当前页面
window.location.replace("http://www.runoob.com")
# 页面自动刷新:把如下代码加入<head>区域中
<meta http-equiv="refresh" content="5">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment