0

I tried with PHP to read JSON-POST-Request, but I get the following error.

Request failed: parsererror

Here is my code

    <script type="text/javascript" charset="utf-8" src="resources/js/model/Product.js"></script>
<script>
    function getdetails(){

        var p = new Product(15,11.5,"Pizza Test","P");
        var z = new Product(68,1.5,"Zutate Test","Z");

        p.addOneProduct(z);

        var request = $.ajax({
            type: "POST",
            url: "yb_test_post.php",
            dataType: "json",
            data: JSON.stringify(p)
        });

        request.done(function(msg) {
            $("#msg").html( " Post parameter are: <br/>"+msg );
        });

        request.fail(function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
        });
    }
</script>

and my PHP-Code, I try to read the POST request and immediately print

    <table>
<?php


    foreach ($_POST as $key => $value) {
        echo "<tr>";
        echo "<td>";
        echo $key;
        echo "</td>";
        echo "<td>";
        echo $value;
        echo "</td>";
        echo "</tr>";
    }


?>
</table>

What do i wrong?

Product.js

function Product(id, price, name, type){
    this.id = id;
    this.price = +price;
    this.totalPrice = +price;
    this.name = name;
    this.type = type;
    this.products = [];
    var self = this;

    this.addOneProduct = function(oneProduct){
        self.products.push(oneProduct);
        self.totalPrice= self.totalPrice+oneProduct.price;
    };
}
0

1 Answer 1

1

You are doing data: JSON.stringify(p). This is sending a JSON string as the post body. You don't want this. PHP will not automatically parse this for you. You want to send PHP a query string, this will make PHP automagically parse it into the $_POST array.

Lose the JSON.stringify, and just try this: data: p.

Also, dataType: "json" is the Content-type of the response, not the request. Your PHP is sending back HTML, so you want: dataType: "html".

var request = $.ajax({
    type: "POST",
    url: "yb_test_post.php",
    dataType: "html",
    data: p
});
Sign up to request clarification or add additional context in comments.

6 Comments

I tried, but now I get the following error message in the web console TypeError: oneProduct is undefined self.totalPrice= self.totalPrice+oneProduct.price;
@user1167253: I was afraid of that. When serializing, jQuery will call functions to get their value. To fix this, add if(!oneProduct){ return '';} as the first line in addOneProduct.
Thx, it's work. But the Send-Format is not a JSON. How can i send to PHP JSON?
@user1167253: Why do you want to send it as JSON? It's better to send it as a query string. If you do it this way, $_POST will populated automagically. If you want JSON, you'd have to parse it manually (I can show you how to do that if you really want).
I can send as a query string, it is not so important. The main thing for me is that I easily convert to Object the data that I get on the server. Is there a solution for this in PHP?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.