从AJAX请求中获取价值 [英] get value from AJAX request

查看:81
本文介绍了从AJAX请求中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这曾经是我的代码:

//At "click" I retrieve value from myFunctionA and use it in myFunctionB
$("#myButton").click(function()
{
    var response = myFunctionA();

    if(response) myFunctionB(response);
});

//Inside myFunctionA I perform an AJAX call (you will see it's really and "SJAX")
function myFunctionA()
{
    var response = myAjax();

    return response;
}

//This is not really and AJAX because of the async:false
function myAjax()
{
    var myJSON = {}

    $.ajax({
        url: "some_url.php",
        dataType: "json",
        async: false,
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert(errorThrown);
        },
        success: function(data, textStatus, jqXHR)
        {
            myJSON = data;
        }
    });

    return myJSON;
}

问题是上面的方法工作得很好.我请求一个值,等待它,然后在需要的地方使用它.显然,不应完成SJAX部分,我必须执行真正的AJAX请求.因此,当我在寻找一种方法来实现这一目标时,我偶然遇到了诸如此类的问题

The thing is that the above worked just fine. I request a value, wait for it, and then use it where I needed. Obviously that SJAX part shouldn't be done, I must do a real AJAX request. So while I was looking for a way to achive this I stumbled with questions like

jQuery:ajax调用成功后返回数据

还有很好的答案,例如

https://stackoverflow.com/a/14220323/702353

,并且还开始阅读有关延迟的以及类似.done()的方法的信息.但是,我无法获得原始SJAX代码的相同结果.所以我的问题是:

and also started to read about Deferred and methods like .done(). However I'm not being able to achieve the same result of my original SJAX code. So my questions are:

  • 是否可以使用AJAX请求执行我想做的事情?
  • 是否可以在不修改.click()代码的情况下完成此操作?
  • Is it possible to do what I want using an AJAX request?
  • Can it be done without modifying the .click() code?

这是我正在使用的当前代码

Here is the current code I'm working on

$("#myButton").click(function()
{
    var response = myFunctionA();

    if(response) myFunctionB(response);
});

function myFunctionA()
{
    $.when(myAjax()).done(function(response)
    {
        return response;
    });
}

function myAjax()
{
    return $.ajax({
        url: "someurl.php",
        dataType: "json",
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert(errorThrown);
        }
    });
}

感谢您的时间!

推荐答案

这是需要做的事情:

$("#myButton").click(function() 
{
    $.when(myFunctionA()).done(function(response)
    {
        myFunctionB(response);
    });    
});

function myFunctionA()
{
    var dfd = new jQuery.Deferred();

    $.when(myAjax()).done(function(data, textStatus, jqXHR)
    {
        dfd.resolve(data);
    });     

    return dfd.promise();    
}

function myAjax()
{
    return $.ajax({
        url: "someurl.php",
        dataType: "json",
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert(errorThrown);
        },
        success: function(data, textStatus, jqXHR)
        {
            return data;
        }
    });
}

上的答案第2部分对我有很大帮助!

This answer on part 2 helped me a lot!

这篇关于从AJAX请求中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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