ajax请求中的Javascript回调函数

时间:2017-04-10 14:21:52

标签: javascript jquery ajax

我正在尝试用js写一个回调函数。问题是返回的值不正确。 ajax调用中的变量“d”包含正确的数据。但是在done(..)函数中的变量a没有。有谁知道如何分配d的值?

    function render_confirmation_email(data, cart, delivery_date){
        console.log("Render confirmation email")
        var purchaseTable = "<table>"
        for (var i = 0; i < cart.length; i++) {
            console.log(i);
            var concept = cart[i].name;
            var price = cart[i].price;
            purchaseTable += "<tr>"
            purchaseTable += "<td>" + concept + " - </td>"
            purchaseTable += "</tr>"
            purchaseTable += "<tr>"
            purchaseTable += "<td>" + price + " kr\n</td>"
            purchaseTable += "</tr>"
        }

        purchaseTable += "</table>"
        purchaseTable += "<br> <p>It will be delivered on " + delivery_date + "</p>"

        var tempDom;
        tempDom = $('<div></div>').append(data);
        tempDom.find('#purchaseTable').append(purchaseTable);
        return tempDom.text()
    }


    function get_confirmation_email(cart, delivery_date, render_confirmation_email) {

        return $.ajax({
            type: "GET",
            url:"/confirmation_email",
            async: false,
            success:function(data) {
                console.log("success");
                // render_confirmation_email called when data is ready
                var d = (render_confirmation_email(data, cart, delivery_date));
                console.log("Rendering done")
                console.log(d)
                return d
            }
        });
    }

    var a = get_confirmation_email(JSONcart, form.querySelector('input[name=deliverydate]').value, render_confirmation_email);
    a.done(function(data) {
        console.log("Function done");
        console.log(data);
    });

谢谢!!!

7 个答案:

答案 0 :(得分:1)

对每个实例使用then()return success不执行任何操作,因为它不属于承诺链

基本示例

function doAjax() {
  // sample data that will be returned
  var json = '{"foo":[1,2,3]}'

  return $.ajax({...}).then(function(resp){
     // return to next "then()" in promise chain 
     return resp.foo
  })
}


doAjax().then(function(data){
  console.log(data) // [1,2,3]
})

DEMO

答案 1 :(得分:0)

您将返回d

var a = get_confirmation_email(JSONcart,form.querySelector('input[name=deliverydate]').value, render_confirmation_email);

您不必使用a.done();

a应该有数据。

答案 2 :(得分:0)

不要复杂化。

    $.ajax({
        dataType: "text",
        type: 'POST',
        url: 'include/ajax.php',
        data: { action: 'functionname', value:value },
        beforeSend:function(){

        },
        success:function(data){


        },
        error:function(){


        }
    });

答案 3 :(得分:0)

jqXHR.done()是一个承诺的回调,将响应数据作为参数传递。您可以将其用作success选项的替代。

var a = get_confirmation_email(JSONcart, form.querySelector('input[name=deliverydate]').value, render_confirmation_email);
a.done(function(data) {
    var d = (render_confirmation_email(data, cart, delivery_date));
});

答案 4 :(得分:0)

我认为您可以将完成的回调传递给get_confirmation_email函数并获取数据。

示例:

function get_confirmation_email(cart, delivery_date, render_confirmation_email,done) {

    return $.ajax({
        type: "GET",
        url:"/confirmation_email",
        async: false,
        success:function(data) {
            console.log("success");
            // render_confirmation_email called when data is ready
            var d = (render_confirmation_email(data, cart, delivery_date));
            console.log("Rendering done")
            console.log(d)
            done(d);
        }
    });
}

get_confirmation_email(JSONcart, form.querySelector('input[name=deliverydate]').value, render_confirmation_email,function(data) {
    console.log("Function done");
    console.log(data);
});

答案 5 :(得分:0)

我认为函数完成回调

.highcharts-color-2 {
    fill: #8E44AD;
    stroke: #8E44AD;
}

和 函数有很多参数。所以你改变了obj的参数。 如下 {   购物车:&#34;&#34;   delivery_date:&#34;&#34;,   ..... }

答案 6 :(得分:0)

function render_confirmation_email(data, cart, delivery_date) {
console.log("Render confirmation email")
var purchaseTable = "<table>"
var tempDom;
tempDom = $('<div></div>').append(data);
tempDom.find('#purchaseTable').append(purchaseTable);
return tempDom.html()
}


function get_confirmation_email(cart, delivery_date, render_confirmation_email) {
    return $.ajax({
        type: "GET",
        url: "/confirmation_email",
        success: function(data) {
            console.log("success");
        }
    }).then(function(resp) {
        return render_confirmation_email(resp, cart, delivery_date)
    });
}

var a = get_confirmation_email(JSONcart, form.querySelector('input[name=deliverydate]').value, render_confirmation_email);
        a.done(function(datababy) {
            // data contains the email
            console.log("Function done");
            console.log(datababy);
        });

我最终得到了这个。谢谢您的帮助! :)

答案:.then()