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

javascript - Ajax JSON.stringify, POST variable is empty - Stack Overflow

programmeradmin1浏览0评论

I'm trying to serialize an HTML form and to send it via Jquery with a POST action, currently I have the following Jquery:

var dataToSend = {
                    'name': 'person',
                    'description': 'very nice person'
                }

                $.ajax({
                    type: "POST",
                    url: "http://localhost/rest/PersonPOST.php",
                    data: JSON.stringify(dataToSend)
                    //contentType: 'application/json',
                    //dataType: 'json'
                });

on the server side I have a PHP script which prints what it receives, so far I managed to receive a request without the $_POST variable. If I dement contentType and dataType nothing changes...

<?php

error_log("START POST");
foreach ($_POST as $key => $entry)
{
    if (is_array($entry))
    {
        foreach ($entry as $value)
        {
            error_log($key . ": " . $value . "<br>");
        }
    }
    else
    {
        error_log($key . ": " . $entry . "<br>");
    }
}

?>

What is wrong with the above ajax request?

EDIT: file_get_contents('php://input') at the server side correctly prints the content which should be inside $_POST variable. Can anyone answer how to normally put this inside the $_POST variable or why it's not possible? thank you

I'm trying to serialize an HTML form and to send it via Jquery with a POST action, currently I have the following Jquery:

var dataToSend = {
                    'name': 'person',
                    'description': 'very nice person'
                }

                $.ajax({
                    type: "POST",
                    url: "http://localhost/rest/PersonPOST.php",
                    data: JSON.stringify(dataToSend)
                    //contentType: 'application/json',
                    //dataType: 'json'
                });

on the server side I have a PHP script which prints what it receives, so far I managed to receive a request without the $_POST variable. If I dement contentType and dataType nothing changes...

<?php

error_log("START POST");
foreach ($_POST as $key => $entry)
{
    if (is_array($entry))
    {
        foreach ($entry as $value)
        {
            error_log($key . ": " . $value . "<br>");
        }
    }
    else
    {
        error_log($key . ": " . $entry . "<br>");
    }
}

?>

What is wrong with the above ajax request?

EDIT: file_get_contents('php://input') at the server side correctly prints the content which should be inside $_POST variable. Can anyone answer how to normally put this inside the $_POST variable or why it's not possible? thank you

Share Improve this question edited Feb 16, 2015 at 14:34 sarah.ferguson asked Feb 16, 2015 at 14:06 sarah.fergusonsarah.ferguson 3,2672 gold badges25 silver badges34 bronze badges 4
  • See stackoverflow./a/18867369 – haim770 Commented Feb 16, 2015 at 14:09
  • Make sure to watch the request / response in your browser's console, it will give you a lot of information on what is going on. – Jay Blanchard Commented Feb 16, 2015 at 14:12
  • 2 Why do you stringify your data? Keep it as an object. Right now, PHP receives a string, not an object. So it can't iterate over it as key/values. – Jeremy Thille Commented Feb 16, 2015 at 14:14
  • file_get_contents('php://input') correctly works, but it's not satisfactory imho. See my edit – sarah.ferguson Commented Feb 16, 2015 at 14:35
Add a ment  | 

1 Answer 1

Reset to default 3

You dont need to stringify since you are already manually creating a JSON object. Try it like this:

var dataToSend = {
    'name': 'person',
    'description': 'very nice person'
};
$.ajax({
    type: "POST",
    url: "http://localhost/rest/PersonPOST.php",
    data: dataToSend
});
发布评论

评论列表(0)

  1. 暂无评论