I have this AJAX:
$.ajax({
url : get_base_url() + 'update',
type : 'POST',
async : false,
data : json_positions,
dataType : 'html',
success : function(data) {
console.log(data);
},
error : function(jqXHR, textStatus, errorThrown) {
console.log('error:');
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown)
}
});
data sent is:
json_positions
which is a string like this:
{"new_positions" : [{ "id": "2", "position": "0"},{ "id": "5", "position": "1"},{ "id": "4", "position": "2"}]}
I want to decode json_positions using json_decode() in a PHP page but it seem that tdata is not sent to the php page because when i try to:
print_r($_POST);
it returns an empty array using console.log(data).
Well your code is okay, no need to change anything. But it’s how you pass your data.
You should not pass it like string. It should be an object like you defimed it:
Make you sure you pass an object, not string, e.g. don’t add any quotes around it etc. Then it’s going to work fine.
EDIT
As per jQuery documentation for $.ajax data parameter:
So your data should be a query string or an object.
In your case I recommend to use object. Like with this code: