Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using jQuery to post a json object to my php application.

jQuery.post("save.php",JSON.stringify(dataToSend), function(data){ alert(data); });

The json string as pulled from firebug looks like this

{ "data" : [ { "contents" : "This is some content",
        "selector" : "DIV.subhead"
      },
      { "contents" : "some other content",
        "selector" : "LI:nth-child(1) A"
      }
    ],
  "page" : "about_us.php"
}

In php I am trying to turn this into an associative array.

My php code so far is

<?php
$value = json_decode(stripcslashes($_POST));
echo $value['page'];
?>

The response to the ajax call should be "about_us.php" but it comes back blank.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
223 views
Welcome To Ask or Share your Answers For Others

1 Answer

$_POST will not be populated if the request body is not in the standard urlencoded form.

Instead, read from the read-only php://input stream like this to get the raw request body:

$value = json_decode(file_get_contents('php://input'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...