jQuery $.get 返回完整对象与我需要的对象
这是我的代码:
var source;
source = $.getJSON(url, function(json) {
return console.log(json);
});
上面返回了完整的 jQuery 对象与我请求的 JSON。响应看起来像这样:
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
isRejected: function () {
isResolved: function () {
overrideMimeType: function ( type ) {
pipe: function ( fnDone, fnFail ) {
promise: function ( obj ) {
readyState: 4
responseText: "{'Hello':'World'}"
setRequestHeader: function ( name, value ) {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( doneCallbacks, failCallbacks ) {
__proto__: Object
有人知道我做错了什么吗?我已经处理这个问题几个小时了:(
Here is my code:
var source;
source = $.getJSON(url, function(json) {
return console.log(json);
});
The above is returning the full jQuery object vs the JSON I am requested. The response looks something like this:
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
isRejected: function () {
isResolved: function () {
overrideMimeType: function ( type ) {
pipe: function ( fnDone, fnFail ) {
promise: function ( obj ) {
readyState: 4
responseText: "{'Hello':'World'}"
setRequestHeader: function ( name, value ) {
status: 200
statusCode: function ( map ) {
statusText: "OK"
success: function () {
then: function ( doneCallbacks, failCallbacks ) {
__proto__: Object
Anyone know what I am doing wrong? I have been dealing with this for a couple of hours now :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AJAX 是异步的,
source
不能等于您请求的 JSON,因为它尚不可用。jQuery.getJSON
将返回它创建的用于处理请求的jqXHR
对象,然后运行并检索响应。当响应可用时(稍后),回调(您传递的函数)将被执行,因此您可以通过第一个参数(您称为 <代码>json)。
您可以通过尝试以下操作来看到这一点:
您将看到控制台将读取 1、3,然后在一瞬间(即执行 HTTP 请求的时间)读取 2;这显示 getJSON 响应未完成就返回,脚本继续执行,然后稍后调用回调。
AJAX is asynchronous,
source
cannot equal the JSON you are requesting, because it isn't available yet.jQuery.getJSON
will return thejqXHR
object it creates to deal with the request and then runs away and retrieves the response.Upon the response being available (some time later), the callback (the function you're passing) is executed, so you can access the response in there via the first parameter (which you've called
json
).You can see this by trying the following:
You'll see that your console will read 1, 3, and then a split second later (i.e. the time to perform the HTTP request), 2; This shows the getJSON response returning without completing, the script continuing to execute, and then the callback being invoked some time later.
从 jQuery 1.5 开始,该方法返回一个 jqXHR 对象(您所看到的)。有关详细信息,请参阅此处的文档。
可以看到你想要的JSON实际上就在responseText变量中。所以它应该是
source.responseText
。As of jQuery 1.5, that method returns a jqXHR object (what you are seeing). See the docs here for more info.
You can see that the JSON you want is actually in the responseText variable. So it should be
source.responseText
.尝试这样做:
Try doing it like this: