129.JQuery中使用AJAX
2023-09-27 14:23:04 时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JQuery中使用AJAX</title>
<!-- 网络导入jQuery -->
<script src="http://code.jquery.com/jquery-3.6.0.js"></script>
</head>
<body>
<h1>JQuery中使用AJAX</h1>
<input type="button" value="发送get请求" onclick="test_get()">
<input type="button" value="发送post请求" onclick="test_post()"><br>
<input type="button" value="发送get请求json" onclick="test_get_args_json()">
<input type="button" value="发送get请求str" onclick="test_get_args_str()"><br>
<input type="button" value="发送post请求json" onclick="test_post_args_json()">
<input type="button" value="发送post请求str" onclick="test_post_args_str()">
<input type="button" value="发送post请求放入data" onclick="test_post_json()"><br>
<input type="button" value="发送请求之前beforesend" onclick="test_beforsend()"><br>
<script>
// get请求访问
function test_get() {
$.ajax({
type:'get',
url:'http://httpbin.org/get',
success:(data)=>{
console.log(data);
}
})
}
// post请求访问
function test_post() {
$.ajax({
type:'post',
url:'http://httpbin.org/post',
success:(data)=>{
console.log(data);
}
})
}
// json形式传递
function test_get_args_json() {
$.ajax({
type:'get',
url:'http://httpbin.org/get',
// 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
data:{
name:'sxt',
pwd:123
},
success:(data)=>{
console.log(data);
}
})
}
// str形式传递
function test_get_args_str() {
$.ajax({
type:'get',
url:'http://httpbin.org/get',
// 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
data:'name=sxt&pwd=123',
success:(data)=>{
console.log(data);
}
})
}
// json形式传递
function test_post_args_json() {
$.ajax({
type:'post',
url:'http://httpbin.org/post',
// 同下,会隐藏请求参数,显示在form中
//form: {name: 'sxt', pwd: '123'} url: "http://httpbin.org/post"
data:{
name:'sxt',
pwd:123
},
success:(data)=>{
console.log(data);
}
})
}
// str形式传递
function test_post_args_str() {
$.ajax({
type:'url: "http://httpbin.org/post"',
url:'http://httpbin.org/post',
data:'name=sxt&pwd=123',
success:(data)=>{
console.log(data);
}
})
}
// 默认情况下data数据放在form下,把data数据以json格式放入data中
function test_post_json() {
$.ajax({
type:'post',
url:'http://httpbin.org/post',
data:JSON.stringify({
name:'sxt',
pwd:123
}),
contentType:'application/json',
success:(data)=>{
console.log(data);
}
})
}
function test_beforsend() {
$.ajax({
type:'get',
url:'http://httpbin.org/get',
// 传递请求参数,会自动拼接 : "url": "http://httpbin.org/get?name=sxt&pwd=123"
data:{
name:'sxt',
pwd:123
},
success:(data)=>{
console.log(data);
},
beforeSend:()=>{
console.log("这是发送请求之前先处理的内容");
// 可以在此函数中控制传送内容,若返回的是false将不会发送请求
return false
}
})
}
</script>
</body>
</html>
相关文章
- Jquery/AJAX实现三级联动
- jquery ajax 请求中 中文汉字的 转码问题
- jquery ajax jsonp跨域调用实例代码
- js/ajax跨越访问-jsonp的原理和实例(javascript和jquery实现代码)
- 使用JQuery Mobile实现手机新闻浏览器
- 基于jQuery的AJAX和JSON的实例
- jquery ajax(实现单独提交某个form)
- Toast信息提示:下拉收起(基于jQuery)(app信息提示更新)
- jQuery Ajax 上传文件夹及文件
- Python之路:Jquery Ajax的使用
- jQuery中ajax的使用与缓存问题的解决方法
- jQuery里面ajax请求的封装
- jquery中的ajax方法参数的用法和他的含义
- 利用jquery对ajax操作,详解原理(附代码)
- jQuery:show()方法
- jquery.form.js 让表单提交更优雅
- Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据
- jquery ajax获取json并解析,获取的json是object对象格式
- javascript、jquery、AJAX总结
- 因为后端 xss 全局过滤器导致的 jquery ajax post 提交的数据全部为null的问题
- Django 基于 jquery 的 ajax
- Web 开发人员不能错过的 jQuery 教程和案例
- jQuery常用事件方法详解
- jquery ajax return值无法获取的解决方法
- 基于jQuery果冻式按钮焦点图切换代码
- 【jquery Ajax 】form表单教学+评论案例