分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

ajax

发布时间:2023-09-06 02:02责任编辑:蔡小小关键词:暂无标签

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‘,‘v1‘);
???data.append(‘k2‘,‘v2‘);
???data.append(‘k3‘,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))

ajax

原文地址:https://www.cnblogs.com/louzi/p/9245858.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved