函数的ajax返回值[重复]

时间:2022-09-10 23:48:17

This question already has an answer here:

这个问题在这里已有答案:

Why doesn't this return a value when the function is called? I've tried it with callbacks, and I can't seem to get it to work either. It lets me console.log, but it just won't return it to the tmp variable.

为什么在调用函数时不返回值?我已经尝试过回调,但我似乎无法让它工作。它允许我console.log,但它不会将它返回到tmp变量。

var URL = "https://api.instagram.com/v1/media/popular?client_id=642176ece1e7445e99244cec26f4de1f";

function getData(){
  var tmp;
    $.ajax({
         type: "GET",
         url: URL,
         dataType: "jsonp",
         success: function(result){
          if(result.meta.code == 200){
            tmp = result.data;
            //console.log(result.data);
          }else{
            alert('fail');
            alert(result.meta.error_message);
          }
         }
    });
    return tmp;
}
console.log(getData()); // undefined?

thanks in advance!

提前致谢!

2 个解决方案

#1


0  

The ajax method is asynchronous. This means that the calling function will continue without waiting for the ajax call to finish.

ajax方法是异步的。这意味着调用函数将继续,而不必等待ajax调用完成。

This has been explained in more depth, here: How do I return the response from an asynchronous call?

这里有更深入的解释,这里:如何从异步调用中返回响应?

#2


0  

I recommend you following method of using Ajax in jQuery

我建议你遵循在jQuery中使用Ajax的方法

"use strict";

var URL = "https://api.instagram.com/v1/media/popular?client_id=642176ece1e7445e99244cec26f4de1f";

$.ajax({
    type: "GET",
    url: URL,
    dataType: "jsonp",
    success: onInstagramSuccess,
    error: onInstagramError
 });

function onInstagramSuccess(result){
    if(result.meta.code == 200){
        console.log(result.data);
    }
}

function onInstagramError(result){
    if(result.meta.code == 404){
        alert('fail');
        alert(result.meta.error_message);
    }
}

Basically Ajax in Javascript is Asynchronous. It's not going to invoke success or fail method until it actually have received a result from the request.

基本上Javascript中的Ajax是异步的。在实际收到请求的结果之前,它不会调用成功或失败方法。

#1


0  

The ajax method is asynchronous. This means that the calling function will continue without waiting for the ajax call to finish.

ajax方法是异步的。这意味着调用函数将继续,而不必等待ajax调用完成。

This has been explained in more depth, here: How do I return the response from an asynchronous call?

这里有更深入的解释,这里:如何从异步调用中返回响应?

#2


0  

I recommend you following method of using Ajax in jQuery

我建议你遵循在jQuery中使用Ajax的方法

"use strict";

var URL = "https://api.instagram.com/v1/media/popular?client_id=642176ece1e7445e99244cec26f4de1f";

$.ajax({
    type: "GET",
    url: URL,
    dataType: "jsonp",
    success: onInstagramSuccess,
    error: onInstagramError
 });

function onInstagramSuccess(result){
    if(result.meta.code == 200){
        console.log(result.data);
    }
}

function onInstagramError(result){
    if(result.meta.code == 404){
        alert('fail');
        alert(result.meta.error_message);
    }
}

Basically Ajax in Javascript is Asynchronous. It's not going to invoke success or fail method until it actually have received a result from the request.

基本上Javascript中的Ajax是异步的。在实际收到请求的结果之前,它不会调用成功或失败方法。