Javascript 在两个页面上发送ajax请求?

Javascript 在两个页面上发送ajax请求?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图在两个php页面上发送ajax post请求,这两个页面是1。properties.php和2。php我正在尝试的代码它在properties.php上发送ajax请求,因此如何在profile.php上发送相同的post请求是我的代码 index.php <div id="div-second-dropdown"></div> <div id="div-third-dropdown"></div> properties.php <?

我试图在两个php页面上发送ajax post请求,这两个页面是1。properties.php和2。php我正在尝试的代码它在properties.php上发送ajax请求,因此如何在profile.php上发送相同的post请求是我的代码

index.php

<div id="div-second-dropdown"></div>
<div id="div-third-dropdown"></div>
properties.php

<?php 

echo $_POST['accountid'];
?>

它在index.php的#div second下拉列表中显示post值

profile.php

<?php 

    echo $_POST['accountid'];
    ?>

它不会在index.php的#div第三个下拉列表中显示post值

只需执行以下操作:

$(document).ready(function() {

    sendAjax();

});


function sendAjax() 
{

    $.ajax(
        {
            url: 'properties.php',
            type: 'post',
            data: 'account-id=' + $('#account-dropdown').val(),
            success: function (html) {
                $('#div-second-dropdown').html(html);
$.ajax(
    {
        url: 'profile.php',
        type: 'post',
        data: {'account-id': $('#account-dropdown').val(),
               'profile-id': $('#profile-dropdown').val()},
        success: function (html) {
            $('#div-third-dropdown').html(html);
        }
    }
);
            }
        }
    );    

}

ajax中的第一个A代表异步,因此第二个请求是在第一个请求完成之前发出的。可能存在会话锁定问题。

尝试在第一个ajax调用的成功回调中调用第二个页面ajax调用

$(document).ready(function () {
   sendAjax();
});

function sendAjax(myUrl) {
    $.ajax({
        url: 'properties.php',
        type: 'post',
        data: 'account-id=' + $('#account-dropdown').val(),
        success: function (html) {
            $('#div-second-dropdown').html(html);
            $.ajax({
                url: 'profile.php',
                type: 'post',
                data: 'account-id=' + $('#account-dropdown').val(),
                success: function (html) {
                    $('#div-second-dropdown').html(html);
                }
            });
        }
    });
}

如果第一次调用成功,您可以利用jquery承诺和玩游戏来执行第二次调用

function sendAjax(dest)
{
    return $.ajax({
        url: dest + '.php',
        type: 'post',
        data: 'account-id=' + $('#account-dropdown').val(),
        success: function (html) {
            $('#div-second-dropdown').html(html);
        },
        error: function(s)
        {
            return s;
        }
    });
}

$(document).ready(function () {
    sendAjax('properties').then( function(){ sendAjax('profile')} );
});

或者,他可以将参数添加到sendAjax函数,即sendAjax(url),并将url设置为ajax的url,然后只需调用该函数任意次数即可。此外,为了处理异步“问题”,他可以在完成第一个函数的回调后调用第二个函数。Echo?成功回调在我发布的代码中也做了同样的事情(因为我不知道你想做什么),所以他们互相竞争,最后完成的人获胜。哈?如果要向其他文件发送另一个请求,只需替换
url:'properties.php'
。我遗漏了什么吗?您可以使用任何类型的循环客户端或向代理php脚本发送请求,然后代理php脚本将为两个脚本服务器端处理该请求问题的标题为“在两个页面上发送”。从我的问题来看,它应该被命名为“发送到两页”。对吗?实际上我有3个下拉列表第二个下拉列表取决于第一个和第三个下拉列表取决于第一个和第二个下拉列表。因为我需要做一些计算,所以我在不同的页面上创建了这些下拉列表,并将它们显示在divs中的index.php上。这似乎需要连续的ajax请求:在1stdropdown中选择value将为2nd设置新值,这将使用1st和2nd中的新值重置3rd。但是你只有在第二个被加载后才有第二个值,这意味着你必须将第三个的请求放入第二个的成功回调中。我讨厌这些“试试这个”答案只是在没有任何解释的情况下抛出一些代码…请在答案上投入一些精力,这样人们就可以从中获得一些东西-而不是复制代码,然后在下一步,类似的问题再次问。“KISSAKI同样在这里……在更新的时候,你指出它…任何WAZ-thNKS:”UMARKHANK OK检查U是否能够正确地调用AJAX。简单地使用我的代码在Ajax调用成功回调中放2个警报,看看u是否得到它们。如果不是PLZ更新你新修改的代码……我会的,如果可以的话。help@iJay我有更新了我的问题第一个ajax调用工作正常,因为它在div中显示值,但第二个ajax调用在索引中的第二个div中不显示值。php@UmarKhan好的..我得到了数据,它在中没有显示值,但是在第二个ajax调用中是否得到了成功响应??
function sendAjax(dest)
{
    return $.ajax({
        url: dest + '.php',
        type: 'post',
        data: 'account-id=' + $('#account-dropdown').val(),
        success: function (html) {
            $('#div-second-dropdown').html(html);
        },
        error: function(s)
        {
            return s;
        }
    });
}

$(document).ready(function () {
    sendAjax('properties').then( function(){ sendAjax('profile')} );
});