1、load方法

使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为:

$(selector).load(URL,data,callback);

参数url为加载服务器地址,可选项data参数为请求时发送的数据,callback参数为数据请求成功后,执行的回调函数。

例子:加载phpinfo.php页面并将放回的结果放到<div>中

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>使用load()方法异步请求数据</title>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
<input id="button" type="button" value="加载"/>
<div id="test"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
$("#test").load("/phpinfo.php",function(responseTxt,statusTxt,xhr){
if(statusTxt=="success")
alert("外部内容加载成功!");
alert(responseTxt);
if(statusTxt=="error")
alert("Error: "+xhr.status+": "+xhr.statusText);
});
});
});
</script>
</body>
</html>

2.getScript()方法

使用getScript()方法异步请求并执行服务器中的JavaScript格式的文件,它的调用格式如下所示:

jQuery.getScript(url,[callback])$.getScript(url,[callback])

参数url为服务器请求地址,可选项callback参数为请求成功后执行的回调函数。

####这个方法就是调用js文件并执行,可以跨域操作#####

a.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
<input id="button" type="button" value="加载"/>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
$.getScript('http://www.123.com:81/2.js',function(){alert("success")})
});
});
</script>
</body>
</html>

2.js

alert('2.js加载成功');  调用成功

执行了回调函数。

3.通过get方法

使用get()方法时,采用GET方式向服务器请求数据,并通过方法中回调函数的参数返回请求的数据,它的调用格式如下:

$.get(url,[callback])

参数url为服务器请求地址,可选项callback参数为请求成功后执行的回调函数。

#########不能跨域访问###############

a.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
$.get("http://www.123.com/",function(data,status){alert("数据:" + data + "\n状态:" + status)});
</script>
</body>
</html>

a.html文件

4.通过post方法

#######不能给跨域############

a.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="html"></div>
<script type="text/javascript">
$.post("http://www.123.com/test.php",
{
name:"afanti",
sex:"man"
},
function(data,status){
alert("数据:" + data + "\n状态:" + status);
document.getElementById("html").innerHTML = data
});
</script>
</body>
</html>

test.php

5.getJSON

$.getJSON("/test.php?name=afanti&sex=man").then(
function(result){
alert(result.name+result.sex);
});

then后面处理回调。getScript也可以使用。

6.ajax发出请求。a.html

$.ajax({
type:"get",
url:"test.php",
data:{"name":"afanti","sex":"man"},
catche: false,
async: false,
timeout: 1000,
dataType:"json", //预期服务器返回的数据类型。支持参数:xml,html,script,json,text,jsonp
contentType: 'application/x-www-form-urlencoded',
   headers:{
  "header1":"aaaa" //设置请求头1
},
   beforeSend:function(xhr){
    xhr.setRequestHeader("header2","bbbb") //设置请求头2
  }
    
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
   //xhrFields:{
   //withCredentials:true //默认情况下,跨域请求不携带cookie。不跨域就可以携带cookie。通过设置这个参数可以实现跨域携带cookie但是后台代码,Access-Contro-Allow-Credentials:true加上这个头部信息
//}
success:function(data,status)
{
document.getElementById("html").innerHTML=data.name+";"+data.sex;
// document.getElementById("html").innerHTML=data;
alert(status);
},
error:function(err,status)
{
alert(status);
}
});

test.php

ajax实现jsonp

http://www.123.com/a.html

$.ajax({
type:"get",
url:"http://www.123.com:81/test.php",
catche: false,
async: false,
timeout: 1000,
dataType:"jsonp", //预期服务器返回的数据类型。
contentType: 'application/x-www-form-urlencoded',
jsonp: 'callback', //http://www.123.com:81/test.php?callback=callback1111&_=1529803521061发出这样的请求
jsonpCallback: 'callback1111', //回调函数的名字
success:function(data,status)
{
for(var key in data)
      {
      console.log(key,data[key]);       } //1、这里
alert(status);
},
error:function(err,status)
{
// alert(status);
}
});
function callback1111(data)
{
alert(data); //2、都能获取到数据
}

www.123.com:81/test.php

<?php
$callback = $_GET['callback'];//得到回调函数名
$data = array('name'=>'afanti','sex'=>'man');//要返回的数据
echo $callback.'('.json_encode($data).')';//输出
?>

可以进行跨域访问。

参考文章:

https://www.xmanblog.net/2018/04/09/web-cross-domain-ecurity/

jQuery中异步请求的更多相关文章

  1. 在内核中异步请求设备固件firmware的测试代码

    在内核中异步请求设备固件firmware的测试代码 static void ghost_load_firmware_callback(const struct firmware *fw, void * ...

  2. SpringBoot中异步请求和异步调用(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...

  3. jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法

    jquery中ajax请求后台数据成功后既不执行success也不执行error,此外系统报错:Uncaught SyntaxError: Unexpected identifier at Objec ...

  4. JavaScrpit中异步请求Ajax实现

    在前端页面开发的过程中,经常使用到Ajax请求,异步提交表单数据,或者异步刷新页面. 一般来说,使用Jquery中的$.ajax,$.post,$.getJSON,非常方便,但是有的时候,我们只因为需 ...

  5. jQuery中ajax请求的六种方法(三、一):$.ajax()方法

    1.基础的$.ajax()方法 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  6. ASP.NET WebForm中异步请求防止XSRF攻击的方法

    在ASP.NET MVC中微软已经提供了如何防止跨域攻击的方法.对于传统Webfrom中使用Handler来接受ajax的Post请求数据,如何来防止XSRF攻击呢.这里给大家提供一个简单地方法,和M ...

  7. jquery Ajax异步请求之session

    写了一个脚本,如下: $(function () { $("#btnVcode").click(function () { var receiveMobile = $(" ...

  8. jQuery Ajax(异步请求)

    jQuery异步请求 原始的异步请求是需要创建的 XMLHttpRequest 对象.(IE5,6不支持)目前很多浏览器都支持XMLHttpRequest对象 jQuery ajax常用的回调函数:b ...

  9. vue中异步请求渲染问题(swiper不轮播)(在开发过程中遇到过什么问题、踩过的坑)

    问题描述: 用vue封装一个swiper组件的时候,发现轮播图不能轮播了. 原因: 异步请求的时间远大于生命周期执行的时间,mounted初始化DOM时数据未返回,渲染数据是空数组,导致轮播图的容器层 ...

随机推荐

  1. js实现点击图片,然后图片放大

    HTML <td width="350"> <img height="100" width="100" class=&qu ...

  2. vscode下eslint代码规范

    直接上规范吧: // 将设置放入此文件中以覆盖默认设置 { "editor.fontSize": 17, "editor.tabSize": 2, " ...

  3. mysql 登录报错:ERROR 1045 (28000)

    公司linux系统的mysql数据库root用户设置过密码,但常常用命令'mysql -u root -p'登录报错,有时又能登录.登录报错信息为: [root@localhost ~]# mysql ...

  4. django中的验证码

    from django.shortcuts import renderfrom PIL import Imagefrom PIL import ImageDrawfrom PIL import Ima ...

  5. Maven环境的搭建

    1.本地仓库和apache-mavenbin.zip的下载与解压 <1.apache-mavenbin.zip下载网址 http://maven.apache.org/download.cgi ...

  6. MySQL一查就会

    Table1--mysql常用操作 主题 用例 说明 书写规范 数据库和表的名称不一定要大写. 输入文本类型的数据时都要加上单引号: NULL 表示未定义,它不会等于另一个NULL: 不要使用双引号. ...

  7. SpringFramework中重定向

    需求: 需要在两个@Controller之间跳转,实现重定向 解决: @PostMapping("/files/{path1}") public String upload(... ...

  8. docker离线安装 启动报错Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.

    安装报错的提示:systemctl status docker.service 好吧,原来是缺少库文件.验证一下想法吧,yum -y install libseccomp 成功后,再启动docker发 ...

  9. 用CSS3 & jQuery创建apple TV海报视差效果

    用CSS和jQuery来实现它,尽量看起来和原效果一样. 最终效果图 本教程里,我将使用CSS,HTML和jQuery来创建一个近似Apple TV视差效果,如果你正在阅读,我假设你对上述三种技术都有 ...

  10. Java设计模式—解释器模式&迭代器模式简介

       解释器模式在实际的系统开发中使用得非常少,因为它会引起效率.性能以及维护等问题,一般在大中型的框架型项目能够找到它的身影,如一些数据分析工具.报表设计工具.科学计算工具等,若你确实遇到" ...