Jquery .ajax method="post" but $_POST empty
$.ajax({
method: "post"
, url: "save.php"
, data: "id=453&act开发者_如何学JAVAion=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 $_GET
or $_REQUEST
but not in $_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
<form method="post" id="myform" action="save.php" enctype="application/x-www-form-urlencoded">
but still $_POST
empty?
How do I make it work?
Instead of method: "post"
you need to use type: "POST"
So this should work without any alterations to your form 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
print_r($_POST);
file.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.
**Add below piece of code in your code before success **
beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
Why not call jQuery.post()
directly?
$.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 POST
request:
$.ajax({
type: "POST",
url: "test.mhtml",
data: $("#myform").serialize(),
success: function(html){
$('#mydiv').html(html);
}
});
This shows up in the Apache logs as:
::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 StackOverflow while 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 POST
or not:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo 'POSTed';
}
?>
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.php
to dir/foo
Apache was dropping the POST request altogether and redirecting.
So when writing the JavasSript you must reference the file in a way that it can bypass any external redirecting.
eg. Drop the extension name inline with the JavaScript.
Write
url: "dir/foo"
instead of
url: "dir/foo.php"
I hope that this helps others who found this page like me.
I was experiencing similar problems with $(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 $_POST
variable 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, $_POST
had something in it (as you might expect!) but more importantly, if ($_POST) { ... }
started returning true
.
In my case the solution was adding the missing .serialize()
in the data
line of the Ajax statement:
data: $("#form").serialize()
You might try changing the contentType, i.e: application/x-www-form-urlencoded
. If you have application/json
for example, you can get the behaviour you witnessed.
$.ajax({
url: 'index.php',
dataType: 'json',
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: { 'test': jsonStr },
success: function( data, textStatus, jQxhr ){
console.log('success');
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
精彩评论