jQuery AJAX/POST 不向 PHP 发送数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22098791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery AJAX/POST not sending data to PHP
提问by C. Ovidiu
So I have this problem for a while now, and I know there are countless questions on this topic, believe me I tried every solution possible but still does not work.
所以我现在有这个问题有一段时间了,我知道关于这个主题有无数的问题,相信我,我尝试了所有可能的解决方案,但仍然不起作用。
This is the simplest of examples which in my case is not working
这是最简单的例子,在我的情况下不起作用
jQuery:
jQuery:
$.ajax({
url: "ajax/add-user.php",
type: "POST",
data: {name: 'John'},
success: function(data){
console.log(data);
}
});
PHP
PHP
echo json_encode($_POST);
That's it.
I always get back an empty array as a response. I tried with serialize()for the data:but the response is always empty. I get no errors whatsoever, just an empty array when I should get the posted values.
就是这样。我总是得到一个空数组作为响应。我试着用serialize()了data:,但响应总是空的。我没有收到任何错误,只有一个空数组,当我应该获取发布的值时。
If, for example, in php I try to echosome hard-coded variables I get them, but the $_POSTand $_GETdon't work.
例如,如果在 php 中,我尝试使用echo一些硬编码的变量来获取它们,但是$_POST和$_GET不起作用。
Any solution or methods of how I can identify the problem ?
我如何识别问题的任何解决方案或方法?
Thank you.
谢谢你。
EDIT/SOLUTION
编辑/解决方案
Ok, so it seems the problem was with .htaccess which rewrites the url and removes the extension. In the network tab indeed the request was moved permanently. Removing the .php extension from the ajax url: solved it. Thank you and sorry for wasting time. Cheers
好的,看来问题出在 .htaccess 上,它重写了 url 并删除了扩展名。在网络选项卡中,请求确实被永久移动了。从 ajax url 中删除 .php 扩展名:解决了它。谢谢你,很抱歉浪费时间。干杯
回答by dreamweiver
You need to set the data type as json in ajax call.
您需要在ajax调用中将数据类型设置为json。
JQUERY CODE:
查询代码:
$.ajax({
url: "ajax/add-user.php",
type: "POST",
dataType:'json',
data: {name: 'John'},
success: function(data){
console.log(data);
}
});
At the same time verify your backend code(php), whether it can accept jsondata ?
同时验证你的后端代码(php),是否可以接受json数据?
If not, configure the header as below:
如果没有,请按如下方式配置标头:
PHP CODE:
代码:
/**
* Send as JSON
*/
header("Content-Type: application/json", true);
Happy Coding :
快乐编码:
回答by Reshape Media
I recently came across this issue but because of other reasons:
我最近遇到了这个问题,但由于其他原因:
- My POST data was redirected as 301
- My Server request was GET
- I did not get my POST data
- 我的 POST 数据被重定向为 301
- 我的服务器请求是 GET
- 我没有得到我的 POST 数据
My reason - I had a url on the server registered as:
我的原因 - 我在服务器上注册了一个网址:
http://mywebsite.com/folder/path/to/service/
But my client was calling:
但我的客户打来电话:
http://mywebsite.com/folder/path/to/service
The .htaccess on the server read this as a 301 GET redirect to the correct URL and deleted the POST data. The simple solution was to double check the url path.
服务器上的 .htaccess 将其读取为 301 GET 重定向到正确的 URL 并删除了 POST 数据。简单的解决方案是仔细检查 url 路径。
@C.Ovidiu, @MarcB - Both of you guys saved me lots of hours in debugging!
@C.Ovidiu、@MarcB - 你们俩在调试中为我节省了很多时间!
回答by BDR
Why not use jQuery.post?
为什么不使用jQuery.post?
$.post("ajax/add-user.php",
{name: 'John'},
function(response){
console.log(response);
}
);
Then in your PHP you could save the input like so:
然后在您的 PHP 中,您可以像这样保存输入:
if(isset($_POST["name"]))
{
$name= $_POST["name"];
file_put_contents("name.txt", $name)
header('HTTP/1.1 200 OK');
}
else
{
header('HTTP/1.1 500 Internal Server Error');
}
exit;
回答by Gavin
I was having this same problem and the issue turned out to be one of my variables in the dataobject was undefined. For some reason that prevented the post data being sent. Setting the variable to something other than undefined fixed the problem.
我遇到了同样的问题,结果是我在data对象中的一个变量未定义。出于某种原因,阻止了发布数据的发送。将变量设置为 undefined 以外的值可以解决问题。
回答by gtryonp
Try to edit your code and use:
尝试编辑您的代码并使用:
$.ajax({url: 'ajax/add-user.php',
type: 'POST',
data: {name: 'John'},
dataType: 'html',
success: function(data){
alert("success");
console.log(data);
},error: function (xhr, ajaxOptions, thrownError) {alert("ERROR:" + xhr.responseText+" - "+thrownError);}
});
(so you will receive an error & success alert, ensure is html what you receive.those extra parameters will help you to fine-tune your ajax calls. Later play with async true/false)
(因此您将收到错误和成功警报,确保您收到的是 html。这些额外的参数将帮助您微调 ajax 调用。稍后使用 async true/false)
also, run your program ajax/add-user.php in a new window (type the url) and see what it echoes.Edit the program and force to echo you something "i.e. legendary hello world" and see if your ajax receive the message.
另外,在新窗口中运行您的程序 ajax/add-user.php(输入 url)并查看它的回声。编辑程序并强制向您回显一些“即传奇的 hello world”,看看您的 ajax 是否收到消息.
Good luck.
祝你好运。
回答by Mahmut Ayd?n
You should check your .htaccess file, close lines with "#" symbol
你应该检查你的 .htaccess 文件,用“#”符号关闭行
# To externally redirect /dir/foo.php to /dir/foo
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
# RewriteRule ^ %1/ [R,L]

