我正在尝试在两个PHP页面上发送ajax post请求,分别是1.属性.php和2.配置文件.php我正在尝试的代码 它在属性上发送 ajax 请求.php那么我如何在配置文件上发送相同的 post 请求.php下面是我的代码
索引.php
<div id="div-second-dropdown"></div>
<div id="div-third-dropdown"></div>
阿贾克斯.js
$(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: 'analytics.php',
type: 'post',
data: 'account-id=' + $('#account-dropdown').val(),
success: function (html) {
$('#div-third-dropdown').html(html);
}
}
);
}
});
}
属性.php
<?php
echo $_POST['accountid'];
?>
它在 #div 秒下拉列表中显示索引.php上的发布值。
简介.php
<?php
echo $_POST['accountid'];
?>
它不会在索引上显示 POST 值.php在 #div-3 下拉列表中
你可以利用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')} );
});
就这样做:
$(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);
}
});
}
});
}