在$ .ajax函数中使用JS变量 - null?

时间:2011-07-05 19:23:36

标签: php javascript jquery ajax variables

我见过很多类似的问题,但我无法找到答案!我正在尝试打开一个PHP文件,使用$ .ajax将一些Javascript变量传递到URL中。但是,当我在Javascript中定义变量然后尝试在$ .ajax中使用它时,它返回null并且未定义变量。我怎样才能传递这个变量?

提前致谢! -C

var searchTerm = "startups";
function Initialize() {
  PopulateTable(searchTerm);
 }
function PopulateTable(searchTerm) {
    $.ajax({  
            type: "POST",
            dataType: "text",
            data:   "tableName=Events&searchTerm=" + searchTerm, 
            // It's not recognizing my JS variables inside Ajax. Has it always been this way?           
            url: "/php/postData.php",  
            success: function(data, textStatus, jqXHR){ 
                alert(data);
            }
        });
    window.location.reload();
}

1 个答案:

答案 0 :(得分:7)

$.ajax({ ...会向您的php资源产生异步请求。 window.location.reload();会在收到请求之前重新加载您的网页,并且可以使用它完成任何操作。如果您需要重新加载页面,请在此处进行:

        success: function(data, textStatus, jqXHR){ 
            alert(data);
            window.location.reload();
        }