php Jquery .ajax method="post" 但 $_POST 为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1650516/
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 method="post" but $_POST empty
提问by TigerTiger
$.ajax({
method: "post"
, url: "save.php"
, data: "id=453&action=test"
, beforeSend: function(){
}
, complete: function(){
}
, success: function(html){
$("#mydiv").append(html);
}
});
I have set method type as post but in Save.php I just get values either in $_GETor $_REQUESTbut not in $_POST.
我有一组方法类型后,但在Save.php我只得到无论是在值$_GET或$_REQUEST而不是在$_POST。
My form looks like:
我的表格看起来像:
<form method="post" id="myform" action="save.php">
It was not working, looked around here and on Google, tried adding enctype
它不起作用,环顾四周和谷歌,尝试添加 enctype
<form method="post" id="myform" action="save.php" enctype="application/x-www-form-urlencoded">
but still $_POSTempty?
但还是$_POST空的?
How do I make it work?
我如何使它工作?
采纳答案by Waleed Amjad
Instead of method: "post"you need to use type: "POST"
而不是method: "post"你需要使用type: "POST"
So this should work without any alterations to your form HTML:
所以这应该可以在不更改表单 HTML 的情况下工作:
$.ajax({
type: "POST"
, url: "save.php"
, data: "id=453&action=test"
, beforeSend: function(){
}
, complete: function(){
}
, success: function(html){
$("#mydiv").append(html);
}
});
Not sure why it doesn't work but this works for me:
不知道为什么它不起作用,但这对我有用:
save.php
保存.php
<?php
print_r($_POST);
file.html
文件.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('input').click(function() {
$.ajax({
type: "POST"
, url: "save.php"
, data: "id=453&action=test"
, beforeSend: function(){
}
, complete: function(){
}
, success: function(html){
alert(html);
}
});
});
});
</script>
</head>
<body>
<div id="main"><input type="button" value="Click me"></div>
</body>
</html>
Your error must lie somewhere else.
你的错误一定在别处。
回答by Rajnish
**Add below piece of code in your code before success **
**在成功之前在您的代码中添加以下代码**
beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
回答by Hyman M.
Why not call jQuery.post()directly?
为什么不jQuery.post()直接打电话?
$.post("save.php",
$("#myform").serialize(),
function(html) {
$("#mydiv").append(html);
},
"html"
);
In regards to jQuery.ajax(), changing to type: "POST"instead of method: "POST"will cause a proper POSTrequest:
关于jQuery.ajax(),更改为type: "POST"而不是method: "POST"将导致正确的POST请求:
$.ajax({
type: "POST",
url: "test.mhtml",
data: $("#myform").serialize(),
success: function(html){
$('#mydiv').html(html);
}
});
This shows up in the Apache logs as:
这在 Apache 日志中显示为:
::1 - - - [30/Oct/2009:09:44:42 -0700] "POST /test.php HTTP/1.1" 200 9 "http://localhost:10501/test.mhtml" "(sic)"
Possible alternate issue:
I found this question on StackOverflowwhile looking around at your problem. Maybe it isn't the jQuery which is giving you trouble, it is PHP? The top voted answer has some suggestions for ensuring that PHP isn't interfering, and the second highest answer offers some code to see if the request is actually a POSTor not:
可能的替代问题:
我在查看您的问题时在 StackOverflow 上发现了这个问题。也许不是 jQuery 给您带来了麻烦,而是 PHP?最高投票的答案提供了一些确保 PHP 不会干扰的建议,第二高的答案提供了一些代码来查看请求是否实际上是一个POST:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo 'POSTed';
}
?>
回答by jkofron.e
I was having trouble with the variables getting lost in the mix as well and discovered that while using Rewrite Conditions to externally redirect dir/foo.phpto dir/fooApache was dropping the POST request altogether and redirecting.
我也遇到了变量在混合中丢失的问题,并发现在使用重写条件从外部重定向dir/foo.php到dir/fooApache 时,会完全丢弃 POST 请求并进行重定向。
So when writing the JavasSript you must reference the file in a way that it can bypass any external redirecting.
因此,在编写 JavasSript 时,您必须以一种可以绕过任何外部重定向的方式引用该文件。
eg. Drop the extension name inline with the JavaScript.
例如。删除与 JavaScript 内联的扩展名。
Write
写
url: "dir/foo"
url: "dir/foo"
instead of
代替
url: "dir/foo.php"
url: "dir/foo.php"
I hope that this helps others who found this page like me.
我希望这能帮助像我一样发现这个页面的其他人。
回答by charlesdeb
I was experiencing similar problems with $(form).ajaxSubmit().
我遇到了类似的问题$(form).ajaxSubmit()。
Browser tools were indicating that I was successfully issuing a POST. On the server $_SERVER['REQUEST_METHOD']was also indicating a POST, and yet the $_POSTvariable in PHP was being returned as array(0) { }. The problem for me was that I was actually submitting an empty form. As soon as there was data in the form, $_POSThad something in it (as you might expect!) but more importantly, if ($_POST) { ... }started returning true.
浏览器工具表明我已成功发出POST. 在服务器$_SERVER['REQUEST_METHOD']上也指示POST,但$_POSTPHP 中的变量被返回为array(0) { }。我的问题是我实际上提交了一个空表格。只要表单中有数据,其中$_POST就有内容(如您所料!)但更重要的是,if ($_POST) { ... }开始返回true.
回答by askepott
In my case the solution was adding the missing .serialize()in the dataline of the Ajax statement:
在我的情况下,解决方案是在 Ajax 语句.serialize()的data行中添加缺少的内容:
data: $("#form").serialize()

