$ .getJSON和$不用彷徨的区别 [英] difference between $.getJSON and $.get

查看:123
本文介绍了$ .getJSON和$不用彷徨的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道真有这两个调用的区别吗?如果您使用的getJSON,你还是要在URL声明格式= JSON ...

Is there really a difference in these two calls? If you use getJSON, you still have to declare format=json in the url...

,您可以在 $这样做。获得(),并遍历JSON对象。

And you can do the same in $.get(), and iterate through the JSON-object.

还是我的方式在这里下车?

Or am I way off here?

推荐答案

下面的两个片段是等价的:

The following two snippets are equivalent:

$.get("/some/url", {data: "value"}, function(json) { 
  // use json here
}, "json")

$.getJSON("/some/url", {data: "value"}, function(json) {
  // use json here
});

话说一个请求是针对 JSON 意味着两件事情:

  • 在jQuery的发送一个接受:应用/ JSON
  • 在jQuery的跨$ P $点入响应,将其转换为JavaScript对象,并将其传递到回调(所以你不必惹的eval或其他转换机制)。

若干服务器端框架(比如Rails)的自动检测接受头和适当地处理请求。如果您使用的是不同的框架或滚动你自己的,你可以检查接受(而不是检查参数)头检测到的格式。

A number of server-side frameworks (such as Rails) automatically detect the Accept header and handle the request appropriately. If you are using a different framework or rolling your own, you can inspect the Accept header to detect the format (instead of inspecting the parameters).

这篇关于$ .getJSON和$不用彷徨的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆