如何从它使用AJAX的JavaScript函数获得价值 [英] How to get value from a javascript function which is using ajax

查看:110
本文介绍了如何从它使用AJAX的JavaScript函数获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话这是越来越用ajax怎么一个值,该值返回到调用函数的函数:
我与Ajax调用的功能是:

 函数getStatusOfAdv(advID){
   $阿贾克斯({
      网址:baseURL时+/管理/广告/ get.adv.status.php
      数据:参数=+越狱(advID)
      数据类型:HTML,
      缓存:假,
      成功:函数(结果){
         返回结果;
      }
   });
}
 

我有这个结果值返回给调用函数是:

 函数setAdvDetail(advID){
VAR状态= getStatusOfAdv(advID);
警报(状态);
}
 

解决方案

$。阿贾克斯()调用是异步的,以便 getStatusOfAdv() Ajax调用返回之前已完成。因此,你必须调整你的code,以便使用异步函数的结果一切code是无论是在阿贾克斯成功处理或从处理程序调用。

您不能做出这样的同步功能 getStatusOfAdv(),使用在其内部调用台异步,然后返回值时, getStatusOfAdv() 的回报。它不能这样做,因为 getStatusOfAdv()返回长异步Ajax调用完成之前,因此返回值还不知道,当它返回。

在JavaScript中的典型设计模式是这样做需要时调用的异步操作完成,数据可在回调函数答案的工作:

 函数getStatusOfAdv(advID,FN){
   $阿贾克斯({
      网址:baseURL时+/管理/广告/ get.adv.status.php
      数据:参数=+越狱(advID)
      数据类型:HTML,
      缓存:假,
      成功:函数(结果){
         FN(结果);
      }
   });
}

功能setAdvDetail(advID){
    getStatusOfAdv(advID,功能(状态){
        警报(状态);
        //使用状态的任何其他code到这里
    });
    //状态不在这里已知的,因为异步函数
    //尚未完成
}
 

I am calling a function which is getting a value using ajax how to return this value to a calling function:
My function with ajax call is:

function getStatusOfAdv(advID){
   $.ajax({
      url:baseURL+"/admin/advertisers/get.adv.status.php",
      data:"param="+escape(advID),
      dataType:"html",
      cache:"false",
      success:function(result){
         return result;
      }
   });
}

I have to return this result value to calling function which is:

function setAdvDetail(advID){
var status = getStatusOfAdv(advID);
alert(status);
}

解决方案

$.ajax() calls are asynchronous so getStatusOfAdv() returns before the ajax call has completed. As such, you will have to restructure your code so that all code that uses the result of the asynchronous function is either in the ajax success handler or is called from that handler.

You can't make a synchronous function like getStatusOfAdv(), use an asychronous call inside it and then return the value when getStatusOfAdv() returns. It cannot be done because getStatusOfAdv() returns long before the asynchronous ajax call has completed, thus the return value is not yet known when it returns.

The typical design pattern in javascript is to do the work that needs the answer in a callback function that is called when the async operation is done and the data is available:

function getStatusOfAdv(advID, fn){
   $.ajax({
      url:baseURL+"/admin/advertisers/get.adv.status.php",
      data:"param="+escape(advID),
      dataType:"html",
      cache:"false",
      success:function(result){
         fn(result);
      }
   });
}

function setAdvDetail(advID){
    getStatusOfAdv(advID, function(status) {
        alert(status);
        // any other code that uses the status goes here
    });
    // the status is NOT known here because the asynch function has
    // not yet completed
}

这篇关于如何从它使用AJAX的JavaScript函数获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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