I'm trying to send a JSON object from an AJAX request to my server.
I'm doing this with JQuery like this:
$.ajax({
type: "POST",
url: settings.ajax.post,
dataType: 'json',
data: basket.aggregate(Basket.EXPORT_JSON, qty),
success: function(data, textStatus, jqXHR) {
if (typeof settings.ajax.success == "function") settings.ajax.success(data, textStatus, jqXHR);
},
error: function(jqXHR, text, e) {
if (typeof settings.ajax.error == "function") settings.ajax.error(jqXHR, text, e);
}
});
The url is pointed to this file on the server:
<?php
$to = "<my-email-address>";
$subject = "JSON test";
$message = "POST dump:\n\n";
foreach($_POST as $key=>$value)
{
$message .= $key . ":" . $value;
}
mail ($to, $subject, $message);
exit;
?>
But the POST var seems to be empty, even though in Firebug I can see that the correct data was sent to the server:
After each request is sent, the ajax error function is called, with an undefined error (I guess because there was no reply from the server? Or I don't know?)
I'm trying to send a JSON object from an AJAX request to my server.
I'm doing this with JQuery like this:
$.ajax({
type: "POST",
url: settings.ajax.post,
dataType: 'json',
data: basket.aggregate(Basket.EXPORT_JSON, qty),
success: function(data, textStatus, jqXHR) {
if (typeof settings.ajax.success == "function") settings.ajax.success(data, textStatus, jqXHR);
},
error: function(jqXHR, text, e) {
if (typeof settings.ajax.error == "function") settings.ajax.error(jqXHR, text, e);
}
});
The url is pointed to this file on the server:
<?php
$to = "<my-email-address>";
$subject = "JSON test";
$message = "POST dump:\n\n";
foreach($_POST as $key=>$value)
{
$message .= $key . ":" . $value;
}
mail ($to, $subject, $message);
exit;
?>
But the POST var seems to be empty, even though in Firebug I can see that the correct data was sent to the server:
After each request is sent, the ajax error function is called, with an undefined error (I guess because there was no reply from the server? Or I don't know?)
Share Improve this question asked Jul 19, 2012 at 23:19 OzzyOzzy 8,3227 gold badges57 silver badges96 bronze badges 9-
1
What does
var_dump($_POST);
output? – uınbɐɥs Commented Jul 19, 2012 at 23:24 - an empty array @ShaquinTrifonoff – Ozzy Commented Jul 19, 2012 at 23:24
- Have a look at this post link – Bishnu Paudel Commented Jul 19, 2012 at 23:31
-
1
You can send data using
.load
. I thought that ifajax
isn't working, then maybeload
would work ? – uınbɐɥs Commented Jul 19, 2012 at 23:37 - 1 @Ozzy Glad you got the answer. – Bishnu Paudel Commented Jul 19, 2012 at 23:38
1 Answer
Reset to default 6POST needs key value pairs, but you're just sending it one value (a JSON string) without a key. It needs to be an array.
Secondly, you need to decode the JSON before you can use it in PHP as an array or object. json_decode()
is used for that.