Home  >  Article  >  Web Front-end  >  jquery中ajax调用json数据的使用说明_jquery

jquery中ajax调用json数据的使用说明_jquery

WBOY
WBOYOriginal
2016-05-16 18:09:13928browse

$.get()和$.post()这两个方法基本都一样使用,所以只说$.post()这个东东!
  情形1: $.post("url",function);此时返回的数据没有处理,所以不是json格式!
  情形2:$.post("url",function, "json");此时虽然指定了返回的数据为json格式,但实际上却不是!
  情形3:$.post("url",{},function);此时传入的参数为空,没有指定返回的数据格式,所以也不是json格式!
  情形4:$.post("url",{},function, "json");正确的返回了json格式的数据!
要点:当要把返回的数据当做的json格式来处理,必须传入参数(参数为空就写成{}),并且还得指定返回类型为"json"!
$.ajax({
url:"url",
dataType:"json",
type:"get"
success:function
})
要点:要指定dataType为"json",此时无论是get还是post方式都会得到json格式数据,但我建议为了和上面的两个方法保持一致,最好加上data:{}这个条件。
$.getJSON("url",function)
要点:以get的方式得到json格式数据,是$.get()的方便写法吧!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn