依赖 jquery的几种AJAX写法

第一种 $.ajax({ 属性名:值,属性名:值})

jquery中功能最全的ajax,写起来相对麻烦

/*
 url:请求服务器地址
 data:请求参数
 dataType:服务器返回数据类型
 error:请求出错执行的功能
 success:请求成功执行的功能(function(data)中data指服务器返回的数据)
 type:请求方式
*/
		$("a").click(function() {
			$.ajax({
				url : 'demo',
				data : {
					"name" : "张三"
				},
				dataType : 'html',
				error : function() {
					alert("请求出错")
				},
				success : function(data) {
					alert("请求成功" + data)
				},
				type : 'POST'
			});
			return false;
		})

第二种(简化$.ajax)

$ .get(url,data,success,dataType))
$ .post(url,data,success,dataType)

第三种(简化$.get())

$.getJSON(url,data,success) 相当于设置 $.get 中 dataType=“json”
$.getScript(url,data,success) 相当于设置 $.get 中 dataType=“script”
若服务器返回数据是从表中取出,为方便客户端操作返回的数据,可把返回数据格式设置成 json

json

客户端把 json当作对象或数组操作
JsonObject:json 对象
{“key”:value,“key”:value}
JsonArray:json 数组
[{“key”:value,“key”:value},{}]

猜你喜欢

转载自blog.csdn.net/sinat_33404263/article/details/105836351