我知道以前有人问过这个问题,但我无法让它工作。
我正在页面上加载一个文件,其路径和名称是 PHP 脚本中包含的变量。
我的脚本.php正在输出一个变量$filename其中包含必须在 ajax 请求中打开的文件的路径。
因此,该文件可以是例如:
'../path/to/file/filea.json' or'../other/path/fileb.json'
我在我的jQuery中尝试过这个:
$.ajax({
url:'script.php',
success:$.ajax({
url: ??? // This ($filename) is what I'm trying to get from the 1st Ajax call
sucess: function(data){
//other code here
}
})
);
我知道$filename第二次 Ajax 调用中是错误的,但是如何获取该变量的值?
$.ajax({
url: "script.php",
success: function(data){
$.ajax({
var someParam=data; //response Data from 1st Ajax Call
type: "post",
url: "example.php",
data: 'page='+someParam,
success: function(data){
//Do More Here!
});
});
试试这个:
在脚本内.php
echo json_encode($filename); //somewhere you need to have this echo.
jQuery
$.ajax({
url: 'script.php',
success: function (data) {
alert(data); //or console.log() and doublecheck you have the right info
$.ajax({
url: JSON.parse(data),
success : function (data) {
//other code here
}
})
}
}); // added a extra } to close the ajax
这解决了问题:
$.ajax({
async:false,
url: 'script.php',
success: function(data){
$.ajax({
url: data,
success: //code here
})
})
@Sergio:脚本.php没有回响$filename的输出...谢谢提醒!