从PHP脚本获取Ajax URL

从PHP脚本获取Ajax URL,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我知道以前有人问过这个问题,但我无法让它工作 我正在加载一个页面上的文件,其路径和名称是包含在PHP脚本中的变量 myscript.php正在输出一个变量$filename,其中包含必须在ajax请求中打开的文件的路径 因此,文件可以是例如: “../path/to/file/filea.json”或 “../other/path/fileb.json” 我在jQuery中尝试了这一点: $.ajax({ url:'script.php', success:$.ajax({

我知道以前有人问过这个问题,但我无法让它工作

我正在加载一个页面上的文件,其路径和名称是包含在PHP脚本中的变量

myscript.php正在输出一个变量$filename,其中包含必须在ajax请求中打开的文件的路径

因此,文件可以是例如:

“../path/to/file/filea.json”或 “../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调用中是错误的,但是如何获取该变量的值呢?

试试以下方法:

inside script.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:script.php没有响应$filename的输出。。。谢谢提醒

您在哪个文件中有此代码?在
script.php
?不,这是在jquery脚本中,您的代码是javascript。那么,如何获得php变量呢?你的问题中缺少一部分。请给出完整的问题描述。你能从php文件中发布一些代码吗?它回应了什么?第二个Ajax中的“example.php”URL是我试图从第一个AjaxPodes aceitar esta resposta:)中得到的,不要使用async:false。
$.ajax({
    async:false,
    url: 'script.php',
    success: function(data){ 
        $.ajax({
        url: data,
            success: //code here
        })
})