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

javascript - Parse json in php after ajax post request - Stack Overflow

programmeradmin1浏览0评论

Suppose there is the javascript with ajax request -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();
var jsonObj = JSON.stringify({dataFeedback: dataFeedback});
$.ajax({
    url: "index.php",
    type: 'POST',
    data: jsonObj,
    dataType: 'json',
    success: function (data) {
        console.log(data);
    }
});

and in the server side , php page -

index.php-

<?php
    $myPostData = json_decode($_POST["dataFeedback"]);
    $feedback = $myPostData["dataFeedback"];
    echo $feedback;
?>

I try to send with the request a json object and once it parsed in the server side, get it back to the client page and logs its data .

In the above concept it doesn't logs the data value .

I checked it in Chrome > F12 > Networks > index.php > Response and found this -

<br />
<b>Notice</b>:  Undefined index: dataFeedback in <b>...\index.php</b> on line <b>11</b><br />

How to cause it to logs the data which backs from the server ?

Update: The code that finally cause it works -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();

$.ajax({
    url: "bin/bll/suggestionSummary.php",
    type: 'POST',
    data: {dataFeedback: dataFeedback},
    success: function (data) {

        console.log(data);
    }
});

index.php-

<?php
    $myPostData = $_POST['dataFeedback'];
    echo $myPostData;

Suppose there is the javascript with ajax request -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();
var jsonObj = JSON.stringify({dataFeedback: dataFeedback});
$.ajax({
    url: "index.php",
    type: 'POST',
    data: jsonObj,
    dataType: 'json',
    success: function (data) {
        console.log(data);
    }
});

and in the server side , php page -

index.php-

<?php
    $myPostData = json_decode($_POST["dataFeedback"]);
    $feedback = $myPostData["dataFeedback"];
    echo $feedback;
?>

I try to send with the request a json object and once it parsed in the server side, get it back to the client page and logs its data .

In the above concept it doesn't logs the data value .

I checked it in Chrome > F12 > Networks > index.php > Response and found this -

<br />
<b>Notice</b>:  Undefined index: dataFeedback in <b>...\index.php</b> on line <b>11</b><br />

How to cause it to logs the data which backs from the server ?

Update: The code that finally cause it works -

index.js-

var dataFeedback = $("#feedback_popup_message_body").val();

$.ajax({
    url: "bin/bll/suggestionSummary.php",
    type: 'POST',
    data: {dataFeedback: dataFeedback},
    success: function (data) {

        console.log(data);
    }
});

index.php-

<?php
    $myPostData = $_POST['dataFeedback'];
    echo $myPostData;
Share Improve this question edited Jun 12, 2014 at 11:37 hsz 152k62 gold badges267 silver badges320 bronze badges asked Jun 12, 2014 at 9:56 URL87URL87 11k36 gold badges111 silver badges177 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

If you have simply value than why are you using JSON.stringify? Try this...

var dataFeedback =  $("#feedback_popup_message_body").val();
var jsonObj  = JSON.stringify({dataFeedback:dataFeedback});
$.ajax({
    url: "index.php",
    type: 'POST',
    data: { dataFeedback:dataFeedback },
    success: function(data){
        console.log(data);
    }
});

Now you can get value direct with $_POST['dataFeedback'];

You're sending raw JSON as the request data, so you can't access it using $_POST on the PHP side, because that only works with form encoded key/value pairs.

You will need to access the raw post data like so, on the PHP side:

$data = json_decode($HTTP_RAW_POST_DATA);
echo $data->dataFeedback;

Finally, you are not outputting JSON on the PHP side, so remove dataType: 'json',, otherwise jQuery will try to parse the response as JSON and invoke the error handler.

jsonObj is a plain string not an array anymore.

What you probably want:

var dataFeedback =  $("#feedback_popup_message_body").val();
    var jsonObj  = JSON.stringify(dataFeedback);
    $.ajax({
        url: "index.php",
        type: 'POST',
        data : {dataFeedback: jsonObj },
        dataType: 'json',
        success: function(data){
            console.log(data);
        }
    });

Now $_POST['dataFeedback'] will be set.

If you want to use associative arrays after json_decode you need to do (notice the second parameter true):

$myPostData = json_decode($_POST["dataFeedback"], true);
发布评论

评论列表(0)

  1. 暂无评论