发送不同ajax请求及iframe

html

 

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<style>
.btn{
display: inline-block;
padding: 5px 10px;
background-color: coral;
color: white;
}
</style>
<body>

<h1>Ajax全套</h1>
<h3>1.Ajax发送GET请求</h3>
<div>
<a class="btn" οnclick="AjaxSubmit1();">点我</a>
<a class="btn" οnclick="AjaxSubmit1();">点我</a>
{# 不依赖jeqery的submit#}
</div>

<h3>2.Ajax发送POST请求</h3>
<div>
<a class="btn" οnclick="AjaxSubmit5();">点我</a>
</div>

<h3>3.莆田</h3>
<div>
<h6>学习iframe</h6>
<div>
<input id="url" placeholder="请输入URL"> <a οnclick="Test1();">查看</a>
</div>
<iframe id="iframe" src="http://www.autohome.com.cn" style="height: 800px;width: 600px"></iframe>
</div>

<div>
<a class="btn" οnclick="AjaxSubmit3();">点我</a>
<a class="btn" οnclick="AjaxSubmit4();">点我</a>
{# 不依赖jeqery的submit#}
</div>


<script src="/static/js/jquery-3.1.1.js"></script>
<script>

function Test1() {
var url=$('#url').val();
$('#iframe').attr('src',url);
}

function AjaxSubmit1() {
$.ajax({
url:'/ajax1.html',
type:'GET',
data:{'p':123},
success:function (arg) {

}
})
}
function AjaxSubmit2() {
{# 创建对象的时候必须加new关键字,jequery是基于xml的一种方式#}
var xhr=new XMLHttpRequest();
xhr.onreadystatechange=function () {
if(xhr.readyState==4){
//接收完毕服务器返回的数据,当每次执行完时值都会改变
console.log(xhr.responseText);
}
};

{# onreadystatechange状态发生更改时会执行,这里要关注全部返回完毕要做的事情。回调函数要在send前配置,不然发时不知道回来找谁#}
xhr.open('GET','/ajax1.html?p=123');
{# 与谁创建连接,以什么方式#}
xhr.send(null);
{# get形式提交就什么都不用放#}
}

function AjaxSubmit3() {
$.ajax({
url:'/ajax1.html',
type:'POST',
data:{'p':123},
success:function (arg) {

}
})
}
function AjaxSubmit4() {
{# 创建对象的时候必须加new关键字,jequery是基于xml的一种方式#}
var xhr=new XMLHttpRequest();

xhr.onreadystatechange=function () {
if(xhr.readyState==4){
//接收完毕服务器返回的数据,回调函数
console.log(xhr.responseText);
}
};
xhr.open('POST','/ajax1.html/');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset-UTF-8');
{# <发送post请求记得携带请求头</发送post请求记得携带>必须把这个带上,把请求从body中转换到post里面#}
xhr.send("p=456");
{# 这时的内容放在request.body中#}
}
{#a. Number readyState#}
{# 状态值(整数)#}
{# #}
{# 详细:#}
{# 0-未初始化,尚未调用open()方法;#}
{# 1-启动,调用了open()方法,未调用send()方法;#}
{# 2-发送,已经调用了send()方法,未接收到响应;#}
{# 3-接收,已经接收到部分响应数据;#}
{# 4-完成,已经接收到全部响应数据;#}
</script>
</body>
</html>


views
from django.shortcuts import render,HttpResponse

# Create your views here.
def index(request):
return render(request,'index.html')

def ajax1(request):
print(request.GET)
print(request.POST)
print(request.body)
return HttpResponse('...')

urls
"""class URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
# https://www.cnblogs.com/wupeiqi/articles/5703697.html
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index.html/$', views.index),
url(r'^ajax1.html/$', views.ajax1),
]

转载于:https://www.cnblogs.com/wenghao/p/11451111.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值