当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jQ获取一部请求的三种方式介绍

jQ获取一部请求的三种方式介绍

2019年05月17日  | 移动技术网IT编程  | 我要评论

1.jq --->$.get()方式获取ajax)

$.get("01.jqueryajaxserver.php","name=liujing&pwd=123456", function (data)
 {
console.log(data);
console.log(data.name);
},"json");

2.jq --->$.post()方式获取ajax

$.post("01.jqueryajaxserver.php","name=liujing&pwd=123456", function (data)
 {
console.log(data);
console.log(data.name);
},"json");

url:发送请求地址。

data:待发送 key/value 参数。

callback:发送成功时回调函数。

type:返回内容格式,xml, html, script, json, text, _default。

如果返回类型声明成script 那么 就算是我们返回的data没有做处理 也会默认当做js代码去执行

html和text 都时按照字符串解析

json 声明服务器返回类型是json

如果是标准的json 则ajax自动帮助我们解析过了(json.parse()) 返回一个对象

如果不是标准的json格式 则获取为"";

3.jq--->$.ajax() 用的最多 因为参数多

//两个参数
 第一个是url 第二个是设置各种参数的对象
$.ajax("02.jqueryforajaxmethod.php",{
success: function (data) {
console.log("返回了!!!");
}
});
一个参数的使用
$.ajax({
url:"02.jqueryforajaxmethod.php",
success: function (data) {
console.log("返回了!!!");
}
});
async:true,//默认就是true 异步
data:{"username":"zhongle","pwd":"123456"},
beforesend:
 function (ajaxobj){}执行时间是在 原生ajax的send方法之前
//
type:get/post
datatype:"json",声明服务器返回数据的类型
 如果是json自动解析 (如果是javascript 自动执行)
如果是html/text 就是字符串
//timeout 设置请求超时时间

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网