ONYX
ONYX

Reputation: 5859

php get access to posted id in ajax request

How do I get access to the data that's been posted via ajax request in my php. My ajax request is working to return a string of data but I can't seem to access the variable thats being passed as a json object. What do I to access a json object that only contains one parameter.

jquery:

$.ajax({
    type:'POST', 
    url:"../../webservices/get_rating.php",
    data:JSON.stringify({product_id:id}),
    dataType:"html",
    success: function(data) {
        $('.ratings-content').append(data);
    }, error:function(data, status, xhr) {
        alert(data + "\r\n" + status + "\r\n" + xhr);   
    }
});

and in my php code its this to get my product_id which doesn't work

PHP:

$product_id = (int)$_POST["product_id"];
echo $product_id; always returns 0

Upvotes: 0

Views: 164

Answers (2)

dt192
dt192

Reputation: 1013

the default for ajax is $_GET so rither specify POST at the ajax end or use GET on the server side

Upvotes: 0

Adeel
Adeel

Reputation: 19238

By default, $.ajax sends data in GET, you need to set Type parameter to POST. Also you did not set the URL, where the data will be POSTED.

Please check this link for more detail.

Upvotes: 2

Related Questions