异步访问

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.3.1.min.js"></script>
<!--<script src="http://libs.baidu.com/jquery/2.1.1/jquery.min.js"></script>-->
<script>
/*AJAX请求非常方便,底层已经封装好了*/
/*实现只修改页面一部分内容而不使页面刷新*/
$(document).ready(function(){
$("#btn").click(function(){
$("#result").text("请求数据中,请稍后");/*提升用户体验*/
$.get("Server2.php",{name:$("#namevalue").val()},function(data){
alert("hello");
$("#result").text(data);
/*这里是get方式,如果想改成post方式直接把js文件中的get和php文件中的get改成post就行了*/
}).fail(function(){
$("#result").text("通讯有问题");
/*视频教程里面用的是.error函数,但是视频是12年的太老了,所以可能会有错误,这里要用fail函数,
* 看来视频也还是要跟着时代走的啊!!!*/
});
});
}).ajaxError(function(event, jqxhr, settings, exception) {
console.log(event);
console.log(jqxhr);
alert(settings.url);/*setting可以获得到底是哪一个请求出错了。*/
// if ( settings.url == "http://localhost:9090/Server1.php" ) {
// $( "#result" ).text( "Triggered ajaxError handler." );
// }
});
</script>
</head>
<body>
<input type="text" id="namevalue">
<br/>
<button id="btn">send</button>
结果:<span id="result"></span>
</body>
</html>

Server.php

<?php
/**
* Created by PhpStorm.
* User: lin
* Date: 2018/12/14
* Time: 14:39
*/
if(isset($_GET['name'])){
echo "hello:".$_GET['name'];
}else{
echo "Args Error(参数错误)";
}

加载片段:

加载片段.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="jquery-3.3.1.min.js"></script>
<script>
$(document).ready(function(){
$("body").text("wait....");
// alert("hello");
$("body").load("box.htm",function(a,status,c){/*加载一个盒子*/
console.log(status);
if(status == "error"){
$("body").text("片段加载失败!");
}
})
$.getScript("helloJS.js",function(){
sayHello();/*这样是可以的,可能是教学视频有点老了,有很多不对应的地方,他居然用了complete方法,
我在官方文档中就没有查到这个方法。*/
/*getScript方法使用.fail()方法处理错误。并不是error方法*/
});
});
/*.ajaxComplete(function( event, xhr, settings ) {
alert(settings.url);
if ( settings.url === "helloJS.js" ) {
/!*这里的setting无法获得helloJS.js的信息,但是能获得box.htm的信息*!/
sayHello();
/!*异步加载的方式就是为了更好的用户体验。
这里可以实现当加载完js文件的时候弹出一个对话框*!/
}
});*/
</script> </head>
<body> </body>
</html>

box.htm

<div style="width:100px; height:100px; background-color: #ff0000"></div>

helloJS.js

function sayHello(){
alert("hello ajax");
}

最新文章

  1. 【PHP面向对象(OOP)编程入门教程】13.访问类型(public,protected,private)
  2. JAVA类与对象(一)----基础概念理解
  3. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!
  4. FromHandle函数
  5. myeclipse 8.5-10.0 安装 svn 方法(转)
  6. UI測试内容
  7. nginx的基础应用(续)
  8. 深入理解Lambda函数及其用法
  9. AngularJS进阶(十二)AngularJS常用知识汇总(不断更新中....)
  10. RabbitMQ 入门【精+转】
  11. [Ubuntu]pkg-config和ldconfig
  12. CentOS 7上VNCServer的安装使用
  13. MySQL数据库引擎MyISAM和InnoDB的区别介绍
  14. css实现垂直居中的方法整理
  15. Kattis之旅——Divisible Subsequences
  16. NOIP2018 No regrets youth
  17. MTK 隐藏上方的状态栏
  18. git remote add origin错误
  19. weblogic基本安装部署
  20. Mac-装机

热门文章

  1. vue:使用element-ui制作动态表格
  2. Python全栈开发:html标签
  3. html--设置复选按钮和单选按钮
  4. 二.ES6新的声明方式
  5. 转载:shell中#*,##*,#*,##*,% *,%% *的含义及用法
  6. php中Cookies
  7. 国内有哪些质量高的JAVA社区?
  8. 反编译之jadx工具
  9. 微信小程序page的生命周期和音频播放及监听
  10. react antd样式按需加载配置以及与css modules模块化的冲突问题