Jquery .ajax方法=“post”但$ _POST为空
$.ajax({ method: "post" , url: "save.php" , data: "id=453&action=test" , beforeSend: function(){ } , complete: function(){ } , success: function(html){ $("#mydiv").append(html); } });
我已将方法类型设置为post但在Save.php中我只是在$_GET
或$_REQUEST
获取值,而不是$_POST
。
我的表格如下:
它无法正常工作,在这里查看并在Google上尝试添加enctype
但仍然是$_POST
空?
我如何使其工作?
而不是method: "post"
你需要使用type: "POST"
所以这应该没有任何改变您的表单HTML:
$.ajax({ type: "POST" , url: "save.php" , data: "id=453&action=test" , beforeSend: function(){ } , complete: function(){ } , success: function(html){ $("#mydiv").append(html); } });
不知道为什么它不起作用但这对我有用:
save.php
file.html
Example
你的错误必须在其他地方。
为什么不直接调用jQuery.post()
?
$.post("save.php", $("#myform").serialize(), function(html) { $("#mydiv").append(html); }, "html" );
关于jQuery.ajax()
,更改为type: "POST"
而不是method: "POST"
将导致正确的POST
请求:
$.ajax({ type: "POST", url: "test.mhtml", data: $("#myform").serialize(), success: function(html){ $('#mydiv').html(html); } });
这在Apache日志中显示为:
::1 - - - [30/Oct/2009:09:44:42 -0700] "POST /test.php HTTP/1.1" 200 9 "http://localhost:10501/test.mhtml" "(sic)"
可能的替代问题:
我在查看你的问题时在StackOverflow上发现了这个问题。 也许不是jQuery给你带来麻烦,它是PHP吗? 最高投票的答案有一些确保PHP不会干扰的建议,而第二个答案提供了一些代码来查看请求是否实际上是POST
:
我也遇到了变量在混合中丢失的问题, dir/foo.php
使用Rewrite Conditions将dir/foo.php
外部重定向到dir/foo
Apache正在完全放弃POST请求并重定向。
因此,在编写JavasSript时,您必须以可以绕过任何外部重定向的方式引用该文件。
例如。 使用JavaScript内联删除扩展名。
写
url: "dir/foo"
代替
url: "dir/foo.php"
我希望这能帮助那些像我一样找到这个页面的人。
**在成功之前在代码中添加以下代码**
beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
我遇到了与$(form).ajaxSubmit()
类似的问题。
浏览器工具表明我已成功发布POST
。 在服务器上, $_SERVER['REQUEST_METHOD']
也表示POST
,但PHP中的$_POST
变量作为array(0) { }
。 对我来说问题是我实际上是在提交一份空表格。 只要表单中有数据, $_POST
有内容(正如您所料!)但更重要的是, if ($_POST) { ... }
开始返回true
。