提交之前的Ajax请求无效

时间:2018-09-12 15:19:19

标签: php jquery ajax

我必须传递一个不在表单中的变量,所以我在提交之前使用了ajax请求

$("#bouton").click(function(){
        dureechoisi = $("#duree1").text().replace(/\D/g,'');
        alert(dureechoisi);
            $.ajax({ 
            type: "POST",
            url: "/iframe/simu-test.php",
            data : {
                dureechoisi1 : dureechoisi
            },
            dataType: 'json',
            xhrFields: {
            withCredentials: true
            },
            async: true
        });
         $("#resultatform").submit(); 
    });

获取我的变量:$dureechoisi = $_POST['dureechoisi1'];

我的post变量为空,我在做什么错?

1 个答案:

答案 0 :(得分:0)

如果在提交之前使用ajax,则在ajax是异步函数的情况下使用ajax的回调。

您可以这样做。

$("#bouton").click(function(){
    dureechoisi = $("#duree1").text().replace(/\D/g,'');
    alert(dureechoisi);
        $.ajax({ 
        type: "POST",
        url: "/iframe/simu-test.php",
        data : {
            dureechoisi1 : dureechoisi
        },
        dataType: 'json',
        xhrFields: {
        withCredentials: true
        },
        async: true,
        success: function() {
            $("#resultatform").submit(); 
        }
    });
});

阅读:https://www.w3schools.com/jquery/ajax_ajax.asp

相关问题