一、Ajax请求

1、jQuery Ajax请求

let ajaxTimeOut = $.ajax({    //将网络请求事件赋值给变量ajaxTimeOut
url: "/api_v1.1/apiPackage/knowKeyWord",
type: "GET",
dataType: "json",
data: {
"knowcontent": this.state.title + this.state.description,
},
timeout: 2000, //通过timeout属性,设置超时时间
success: function (data) {
console.log("获取关键字成功");
console.log(data);
}.bind(this),
error: function (xhr, status, err) {
}.bind(this),
complete: function (XMLHttpRequest, status) { //当请求完成时调用函数
if (status == 'timeout') {//status == 'timeout'意为超时,status的可能取值:success,notmodified,nocontent,error,timeout,abort,parsererror
ajaxTimeOut.abort(); //取消请求
Modal.warning({ //超时提示:网络不稳定
title: '友情提示',
content: '网络不稳定',
});
}
}
}); 

2、jQuery Ajax请求

       $.ajax({
url:"https://heidongbuhei.github.io/test/pages/"+pagrUrl,
type:"get",
data:{},
async:true,
dataType:"html",
beforeSend:function(){
$(".loading-box").show();
},
complete:function(){
$(".loading-box").hide();
},
success:function (data) {
console.log(data);
$(".change-part").html(data); },
error:function (data) {
console.log(data.status);
}
})

跨域支持:

dataType: 'jsonp',
crossDomain: true,

代码如下:

$(document).ready(function(){
$("button").click(function(){
$.ajax({
url:"http://t.weather.sojson.com/api/weather/city/101220101",
type:"get",
data:{},
async:true,
dataType: 'jsonp',
crossDomain: true,
beforeSend:function(){
$(".loading-box").show();
},
complete:function(){
$(".loading-box").hide();
},
success:function (data) {
console.log(data);
$(".change-part").html(data); },
error:function (data) {
console.log(data.status);
}
})
});
});

按钮:

<button>切换</button>

二、jQuery.extend

1、jQuery.extend类方法:Query本身的扩展方法。

<script>
$.extend({
print1:function(name){ //print1是自己定义的函数名字,括号中的name是参数
console.log(name)
}
});
$.print1("坤") ; //调用时直接$.函数名(参数);
</script>

2、jQuery.fn.extent()对象方法: jQuery 所选对象扩展方法。

<body>
<input type="text">
<script>
$.fn.extend({
getMax:function(a,b){
var result=a>b?a:b;
console.log(result);
}
});
$("input").getMax(,); //调用时要$(标签名).函数名();
</script>
</body>

3、一般情况下,jQuery的扩展方法写在自执行匿名函数中(原因:在js中是以函数为作用域的,在函数中写可以避免自己定义的函数或者变量与外部冲突)。

<script>
(function(){
$.extent({
print1:function(){
console.log();
}
})
})();
</script>

三、JavaScript prototype 属性

prototype 属性允许您向对象添加属性和方法

注意: Prototype 是全局属性,适用于所有的 Javascript 对象。

(function($){
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + , //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$, (this.getFullYear() + "").substr( - RegExp.$.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
})(jQuery);

使用方式:

(new Date()).Format("yyyy-M-d h:m:s")

输出结果:

2018-06-07 23:26:22

四、JQuery关于(function($){ })(jQuery)匿名函数的解析

(function($){ })(jQuery):执行(jQuery)函数,并把jQuery对象作为实参,然后就会自动调用(function ($) {...}()这个匿名函数,并把实参传递给匿名函数,作为匿名函数的形参。
(function ($) {
alert("我执行了");
})(jQuery);

等同于

function callfunc($) {
alert("我执行了");
}
callfunc(jQuery);

效果演示:

事例演示

相当于定义了一个参数为info的匿名函数,并且执行("CoderZB")的同时,将CoderZB作为参数来,就会自动调用这个(function (info) {})匿名函数.最后面的()是调用匿名函数并将参数传递给匿名函数
(function (info) {
alert(info);
})("CoderZB");

其实就是这种形式。

function infomationFunc(info) {
alert(info);
};
infomationFunc("CoderZB");

执行结果

事例演示

二、jQuery Ajax请求的更多相关文章

  1. jquery.ajax请求aspx和ashx的异同 Jquery Ajax调用aspx页面方法

    1.jquery.ajax请求aspx 请求aspx的静态方法要注意一下问题: (1)aspx的后台方法必须静态,而且添加webmethod特性 (2)在ajax方法中contentType必须是“a ...

  2. jquery ajax请求方式与提示用户正在处理请稍等,等待数据返回时loading的显示

    1.jquery ajax请求方式与提示用户正在处理请稍等 为了提高用户体验度,我们通常会给出 “正在处理,请稍等!”诸如此类的提示.我们可通过设置$.ajax()下的参数beforeSend()来实 ...

  3. jQuery ajax 请求php遍历json数组到table中

    html代码(test.html),js在html底部 <!DOCTYPE html> <html lang="en"> <head> < ...

  4. jquery Ajax请求示例,jquery Ajax基本请求方法示例

    jquery Ajax请求示例,jquery Ajax基本请求方法示例 ================================ ©Copyright 蕃薯耀 2018年5月7日 https: ...

  5. 原生js,jquery ajax请求以及jsonp的调用

    ajax    是用来处理前后端交互的技术,可以改善用户体验,其本质是    XMLHttpRequest,异步访问服务器并发送请求数据,服务器返回响应的数据,以页面无刷新的效果改变页面中的局部内容  ...

  6. jQuery ajax请求错误返回status 0和错误error的问题

    上周发现一个jQuery ajax请求错误返回status 0和错误error的问题,responseText是"error",状态码是0而不是200: $.ajax({ type ...

  7. jquery Ajax请求中显示Loading...

    jquery Ajax请求中显示Loading... $('#btnTest').click(function(){      $.ajax({           url ---- ,根据你需要设置 ...

  8. JQuery Ajax 请求参数 List 集合处理

    引言 JQuery Ajax 发送请求参数一般都是基本类型,比如 String.int:那么,请求参数如果是 List 集合应该如何处理呢? 情况一:Aajx 发送 List 类型请求参数 举例如下: ...

  9. jquery ajax 请求参数详细说明 及 实例

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

随机推荐

  1. Codeforces Round #554 (Div. 2) C.Neko does Maths (gcd的运用)

    题目链接:https://codeforces.com/contest/1152/problem/C 题目大意:给定两个正整数a,b,其中(1<=a,b<=1e9),求一个正整数k(0&l ...

  2. Vue组件-组件组合

    组件设计初衷就是要配合使用的,最常见的就是形成父子组件的关系:组件 A 在它的模板中使用了组件 B. <html> <head> <title>Vue组件 A 在它 ...

  3. ubuntu安装Scala

    参考地址:https://blog.csdn.net/qq_36743482/article/details/78317549 1.下载Scala:http://www.scala-lang.org/ ...

  4. axios之前端发送get与post请求模板

    import axios from "axios"; 一.get mounted() { axios .get( "/api/queryusertree?domId=&q ...

  5. webpack CSS处理loader

    loader概念: 首先来介绍一下loader,之前我们用webpack来处理我们写的js代码,并且webpack会自动处理js之间相关的依赖.但是,在开发中我们不仅仅有基本的js代码处理,我们也需要 ...

  6. 01 spring security入门篇

    1. 环境搭建 使用SpringBoot搭建开发环境,只需在pom.xml添加如下依赖即可. <?xml version="1.0" encoding="UTF-8 ...

  7. Java自定义注解学习

    1.定义一个枚举类,后面自定义注解时使用 package cn.tx.annotation.enums; /** * 定义枚举类型 * @author Administrator * */ publi ...

  8. Mybatis的运行原理

    我们写的sql语句就在statementBuilder中. 整个mapper的信息都保存到configuration 1:根据配置文件创建SQLSessionFactory 先创建SqlSession ...

  9. KVM(多电脑切换器)

    KVM:Keyboard Video Mouse的缩写.KVM 交换机通过直接连接键盘.视频和鼠标 (KVM) 端口,让您能够访问和控制计算机.KVM 技术无需目标服务器修改软件.这就意味着可以在 W ...

  10. [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)

    题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...