从ajax返回数据会导致奇怪的对象 [英] Returning data from ajax results in strange object

查看:163
本文介绍了从ajax返回数据会导致奇怪的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题可能已被问了一千次,但我似乎无法找到答案。我希望 result 是从ajax-request返回的数据,它应该是一个json数据数组( console.log的结果(数据) ))。

I know this question hast probably been asked a thousand times, but i cannot seem to find the answer. I want result to be the data returned from the ajax-request, which should be a json-data array (the result of console.log(data)).

  var result = $.ajax({
    type: 'GET',
    url: dataPath,
    dataType: 'json',
    success: function(data) {
      console.log(data)
      },
    error: function(){
      //alert("damn");  
      },
    data: {},
    aync: false
  });

  console.log(result); 

然而, console.log(结果); 将返回一些奇怪的物体,我不知道如何处理。为什么不是结果 = 数据

However, console.log(result); will return some strange object, which I don't know how to handle. Why isn't result = data ?

推荐答案

首先从代码中删除 aync:false 。它应该拼写 async:false 但你不需要它来实现你的目标,它实际上做的是阻止整个浏览器的用户界面,导致糟糕的用户体验。请记住,AJAX中的A表示异步

First of all remove the aync: false from your code. It should be spelled async: false but you don't need it to achieve your goal and what it actually does is block the entire browser's user interface resulting in a terrible user experience. Remember that "A" in AJAX means Asynchronous.

$。ajax()来电是承诺,这与您的数据不同,但它仍然可用于获取您的数据。您只需要以某种方式使用它。

The result of an $.ajax() call is a promise which is not the same as your data, but it can still be useful to get to your data. You just need to use it in a certain way.

尝试更改:

 console.log(result);

to:

result.done(function (data) { 
    console.log(data);
});

或:

result.done(function (data) { 
    console.dir(data);
});

甚至可能有效 - 未经测试:

or even this might work - untested:

result.done(console.dir);

参见 此回答 以获得更好的解释。

See this answer for a better explanation.

这篇关于从ajax返回数据会导致奇怪的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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