jQuery $.get 返回完整对象与我需要的对象

发布于 2024-12-10 12:00:58 字数 848 浏览 0 评论 0原文

这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-12-17 12:00:58

AJAX 是异步的,source 不能等于您请求的 JSON,因为它尚不可用。 jQuery.getJSON 将返回它创建的用于处理请求的 jqXHR 对象,然后运行并检索响应。

$.getJSON(url, function(json) {
  // deal with response  here
});

当响应可用时(稍后),回调(您传递的函数)将被执行,因此您可以通过第一个参数(您称为 <代码>json)。

您可以通过尝试以下操作来看到这一点:

console.log('1');

$.getJSON(url, function(json) {
    console.log('2');
});

console.log('3');

您将看到控制台将读取 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 the jqXHR object it creates to deal with the request and then runs away and retrieves the response.

$.getJSON(url, function(json) {
  // deal with response  here
});

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:

console.log('1');

$.getJSON(url, function(json) {
    console.log('2');
});

console.log('3');

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.

风渺 2024-12-17 12:00:58

从 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.

岛歌少女 2024-12-17 12:00:58

尝试这样做:

var source;
source = $.get(url, function(data) {
  return alert($.parseJSON(data));
});

Try doing it like this:

var source;
source = $.get(url, function(data) {
  return alert($.parseJSON(data));
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文