无法在php文件中获取js变量
无法在无法在php文件中获取js变量,php,jquery,ajax,Php,Jquery,Ajax,无法在msg.php中获取a $('#click01').click(function(){ var a = 14; $.ajax({ url : 'msg.php', type : 'post', data : {a:a} }).done(function() { window.location='msg.php'; }) }); msg.php var_dump($_POST['a']); // NULL, but i need
msg.php中获取a
$('#click01').click(function(){
var a = 14;
$.ajax({
url : 'msg.php',
type : 'post',
data : {a:a}
}).done(function() {
window.location='msg.php';
})
});
msg.php
var_dump($_POST['a']); // NULL, but i need `14`
您得到的是null
,因为AJAX调用完成后,您将用户发送到msg.php
,其中$\u POST
再次为空
当您这样做时:
$('#click01').click(function () {
var a = 14;
$.ajax({
url: 'msg.php',
type: 'post',
data: {
a: a
}
}).done(function (data) {
alert(data);
})
});
你会看到它的工作,你得到14
我看不出有什么好的理由首先使用AJAX发布内容,然后成功地将用户发送到同一页面。您将获得null
,因为AJAX调用完成后,您将用户发送到msg.php
,其中$\u POST
再次为空
当您这样做时:
$('#click01').click(function () {
var a = 14;
$.ajax({
url: 'msg.php',
type: 'post',
data: {
a: a
}
}).done(function (data) {
alert(data);
})
});
你会看到它的工作,你得到14
我看不出有什么好的理由首先使用AJAX发布内容,然后成功地将用户发送到同一页面。您对AJAX请求的看法不好
$('#click01').click(function(){
var a = 14;
$.ajax({
url : 'msg.php',
type : 'post',
data : {a:a}
}).done(function(data) {
// data= var_dump($_POST['a'])
alert(data)
})
});
在var数据中,您有ajax请求的结果,但是如果您重定向到msg.php no并没有变量,那么您对ajax请求的理解就不好了
$('#click01').click(function(){
var a = 14;
$.ajax({
url : 'msg.php',
type : 'post',
data : {a:a}
}).done(function(data) {
// data= var_dump($_POST['a'])
alert(data)
})
});
在var数据中,您有ajax请求的结果,但是如果您重定向到msg.php no,则没有变量,您确定您正在检查ajax返回,而不是窗口的返回。location='msg.php'代码>
试试这个:
$('#click01').click(function(){
var a = 14;
$.ajax({
url : 'msg.php',
type : 'POST',
data : {'a':a},
success: function(r) { alert(r); }
});
});
您确定检查的是ajax返回,而不是window.location='msg.php'的返回吗代码>
试试这个:
$('#click01').click(function(){
var a = 14;
$.ajax({
url : 'msg.php',
type : 'POST',
data : {'a':a},
success: function(r) { alert(r); }
});
});
//在msg.php中使用以下命令代替var_dump($_POST['a']):
//摘录(元);;echo$a
$('document').ready(function(){var a=14
$.ajax({
url : "msg.php",
type : 'post',
data : {a:a}
}).done(function(data) {
alert(data);
});
});
//在msg.php中使用以下命令代替var_dump($_POST['a']):
//提取($\u POST);回显$a
$('document').ready(function(){var a=14
$.ajax({
url : "msg.php",
type : 'post',
data : {a:a}
}).done(function(data) {
alert(data);
});
});
你怎么知道它是空的?试着打印$_REQUEST@putvande,使用var\u dump
-NULL
写入显示它确实使用echo或print而不是var\u dump从ajaxtry发送14您如何知道它为NULL?尝试打印$_REQUEST@putvande,使用var\u dump
-NULL
写入displayit14从Ajax尝试使用echo或print而不是var_dump