Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

javascript - Returning data from ajax results in strange object

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); 

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Typo.

Change this:

aync: false

to:

async: false

And the ajax method still returns the jqXHR object doing the request, not the result. Use the data parameter in the success call and store it somewhere.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...