Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript, Ajax ,php parsererror, and How get post Json?

I got this code in game.php to refresh .refre, but I always get "parsererror". jsonString is from jsonString= JSON.stringify(object);

<div class="refre"></div>..
<script>...
        jsonString ={"right":0,"arrayQ":[{"0":"10","idQ":"19"},
            {"0":"34","idQ":"20"},{"0":"89","idQ":"14"}],"lives":40};

    $.ajax({
        type: 'POST',
        data: jsonString,
        dataType: 'json',
        url: 'Q.php',
        contentType : 'application/json; charset=utf-8'

    }).done( function (data, status) {
        $('.refre').load('Q.php');
        alert('Right');
    })
    .fail( function (data, status) {
        alert("Wrong: "+status);
    });
        ...</script>

The other file is Q.php, this read the POST and send some HTML, Now It is just for check the information of the POST.

<?php
$value = json_decode($_POST);
$categories = json_decode(file_get_contents('php://input'));

print_r($value);
print_r($categories);

?>

What's wrong with the ajax?? how I get the POST in Q.php? how can I get 'lives' from the JSON in Q.php?

like image 324
RaccoonCode Avatar asked Nov 16 '25 22:11

RaccoonCode


1 Answers

Try this,

<div class="refre"></div>..
<script>...
        jsonString =[{"right":0,"arrayQ":[{"0":"10","idQ":"19"},
            {"0":"34","idQ":"20"},{"0":"89","idQ":"14"}],"lives":40}];

    $.ajax({
        type: 'POST',
        data: jsonString,
        dataType: 'html',
        url: 'Q.php',
        contentType : 'application/json; charset=utf-8'

    }).done( function (data, status) {
        $('.refre').html(data);
        alert('Right');
    })
    .fail( function (data, status) {
        alert("Wrong: "+status);
    });

    ...</script>

<?php
$value = json_decode($_POST);
$categories = json_decode(file_get_contents('php://input'));

print_r($value);
print_r($categories);

?>
like image 139
Pranav C Balan Avatar answered Nov 19 '25 11:11

Pranav C Balan