[英]Why am I not receiving variable from JS to PHP?
I am trying to send a variable from JS to php through ajax but I'm not able to get in php file. 我正在尝试通过ajax将变量从JS发送到php,但无法进入php文件。
JS JS
var names = ['lee','carter'] ;
$.ajax({
type: "POST",
url: "http://localhost/test/ajax.php",
data: {name:names},
}).done(function() {
location.href = 'http://localhost/test/ajax.php' ;
});
PHP 的PHP
print_r($_POST);
this is showing an empty array but when I do console.log(data) it shows an array in console.log 这显示了一个空数组,但是当我执行console.log(data)时,它在console.log中显示了一个数组
var names = ['lee','carter'] ;
$.ajax({
type: "POST",
url: "http://localhost/test/ajax.php",
data: {name:names},
}).done(function(data) {
console.log(data) ;
});
Edit: (by mega6382) I believe OP wants to open a page in browser with post params, which cannot be done by AJAX. 编辑:( 按mega6382)我相信OP希望在浏览器中使用post params打开页面,而AJAX无法完成。 All others who answered got mistaken by the AJAX code in the question and started providing AJAX solutions, without realizing what OP is trying to do. 回答问题的所有其他人都被问题中的AJAX代码所误解,并开始提供AJAX解决方案,而没有意识到OP试图做什么。 If you were to read OP's comments on Jeroen's answer. 如果您要阅读OP对Jeroen答案的评论。
The problem is with what you do when the ajax request finishes: 问题在于ajax请求完成时的操作:
}).done(function() {
location.href = 'http://localhost/test/ajax.php' ;
});
Here you are re-directing to http://localhost/test/ajax.php
(requesting it a second time...), using a GET request so $_POST
is indeed empty. 在这里,您使用GET请求$_POST
到http://localhost/test/ajax.php
(第二次请求...),因此$_POST
确实为空。
只需在您的php文件中执行以下操作即可接收json格式的字符串
echo json_encode(['success' => true]);
Instead of ajax try sending a dynamically generated form like: 代替ajax,尝试发送动态生成的表单,例如:
var names = ['lee','carter'] ;
var newForm = $('<form>', {
'action': "http://localhost/test/ajax.php",
'target': '_top',
'method': 'POST'
});
names.forEach(function (item, index)
{
newForm.append($('<input>', {
'name': 'name[]',
'value': item,
'type': 'hidden'
}));
});
$(document.body).append(newForm);
newForm.submit();
This will send the values over POST via a form. 这将通过表单通过POST发送值。 It will do both redirect to the new page and send post vals. 它将同时重定向到新页面并发送后价。
Since you're using an AJAX
request, in this case POST
, you could use the _REQUEST
method in php for obtaining the JS variable. 由于您使用的是AJAX
请求,在本例中为POST
,可以在php中使用_REQUEST
方法来获取JS变量。 In your case try: 在您的情况下,请尝试:
$names = $_REQUEST['name']
echo $names;
/* Or try JSON_ENCODE if echo isn't working*/
$json = json_encode($names)
echo ($json);
var names = ['lee','carter'] ;
JSON.stringify(names);
$.ajax({
type: "POST",
dataType: 'json',
url: "http://localhost/test/ajax.php",
data: {name:names}
}).done(function() {
console.log('ok');
});
This is a successful ajax call. 这是一个成功的ajax调用。 Now in order to "check by yourself" that is working you don't have to redirect to the other page because that way you refresh and lose the data. 现在,为了“自己检查”是否有效,您不必重定向到另一页,因为那样可以刷新并丢失数据。 What you have to do is: 您要做的是:
This link is to help you understand how to debug ajax call 该链接可帮助您了解如何调试Ajax调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.