最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to receive JSON object in PHP? - Stack Overflow

programmeradmin3浏览0评论

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 if ajax isn't working, then maybe load 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
 |  Show 4 more ments

1 Answer 1

Reset to default 6

POST 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.

发布评论

评论列表(0)

  1. 暂无评论