加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax

发布时间:2020-12-16 03:03:49 所属栏目:百科 来源:网络整理
导读:jquery_ajax: function ajaxClick1() { $.ajax({ url:‘ajax1.html‘, type:‘GET‘, data:{‘p‘:123}, success:function (arg) { console.log(arg) } }) } 原生ajax GET请求: function ajaxClick2() { var xhr = new XMLHttpRequest(); //创建对象 xhr.on

jquery_ajax:

function ajaxClick1() {
$.ajax({
url:‘ajax1.html‘,
type:‘GET‘,
data:{‘p‘:123},
success:function (arg) {
console.log(arg)
}
})
}

原生ajax
GET请求:

function ajaxClick2() {
var xhr = new XMLHttpRequest(); //创建对象
xhr.onreadystatechange = function() { //模拟jquery回调函数
if (xhr.readyState){
console.log(xhr.responseText)
}
};
xhr.open(‘GET‘,‘/ajax1.html?p=123‘);
xhr.send(null);
}

django POST请求:
function ajaxClick4() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState){
console.log(xhr.responseText)
}
};
xhr.open(‘POST‘,‘/ajax1.html‘);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(‘p=123‘);
}

iframe仿造ajax请求
html:

<h3>iframe伪造ajax</h3>
<iframe name="ifra" id="iframe"></iframe>
<form id="fm" action="ajax1.html" method="post" target="ifra">
<input type="text" name="name">
<a onclick="ajaxClick5()">提交</a>
</form>

js:

function ajaxClick5() {
document.getElementById(‘iframe‘).onload = iframeClick;
document.getElementById(‘fm‘).submit();
}

function iframeClick() {
var ifr_load = this.contentWindow.document.body.innerHTML;
var obj = JSON.parse(ifr_load);
if (obj.status){
alert(obj.message)
}
}

jquery_ajax上传文件
function ajaxUpload() {
var data = new FormData(); 创建FROMDATA对象
data.append(‘k1‘,‘v1‘);
data.append(‘k2‘,‘v2‘);
data.append(‘k3‘,document.getElementById(‘files‘).files[0]);
$.ajax({
url:‘/ajax1.html‘,
type:‘POST‘,
data:data,
success:function (arg) {
console.log(arg);
},
processData:false,
contentType:false
})
}


原生ajax上传文件:

function ajaxUpload1() {
var data = new FormData();
data.append(‘k1‘,document.getElementById(‘files‘).files[0]);
var xhr = new XMLHttpRequest();
xhr.open(‘POST‘,‘/ajax1.html‘);
xhr.onreadystatechange = function () {
if (xhr.readyState){
console.log(xhr.responseText)
}
};
xhr.send(data);
}



django不通过FORM组件上传文件:

def ajax1(request):    data = {‘status‘:True,‘message‘:‘...‘}    print(request.GET)    print(request.POST)    file_name = request.FILES[‘k3‘]    with open(‘123‘,‘wb‘) as file:        for i in file_name.chunks():            file.write(i)    import json    return HttpResponse(json.dumps(data))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读