Reputation: 6664
My PHP script is having a problem decoding the JSON that I'm sending to it via AJAX.
The jQuery:
$.ajax({
url : 'admin/modifyPermissions',
type : 'post',
data : {
'JSON' : JSON
},
success : function(msg){
if(msg == '1') {
alert('Permissions saved successfully');
} else {
alert(msg);
}
}
});
The PHP script:
public function modifyPermissions(){
if(isset($_POST['JSON']) && !empty($_POST['JSON'])) {
$json = json_decode($_POST['JSON'],true);
if($json !== NULL && $json !== FALSE) {
} elseif($json === NULL){
die('The string passed is not a valid JSON object and cannot be decoded.' . "\n" . $_POST['JSON']);
} else {
die('There was an error with the JSON string');
}
} else {
die('No JSON string was sent!');
}
}
The JSON that gets passed looks well formed to me:
{"martin":{3,5},"user2":{3,4,5}}
And PHP is returning null. I have PHP 5.2.7 installed on my server, so I can't use json_last_error()
Upvotes: 0
Views: 257
Reputation: 87073
{"martin":{3,5},"user2":{3,4,5}}
Not valid JSON. Valid JSON may look like this:
{"martin":[3,5],"user2":[3,4,5]}
Upvotes: 5
Reputation: 4830
Your JSON is invalid.
The {}
notation denotes key/value pairs, where as you're using it as an array.
Your JSON should be,
{"martin":[3,5],"user2":[3,4,5]}
Upvotes: 0
Reputation: 60717
You're not sending valid JSON, thus the error. Look at the comment @Matt added.
So that you won't reproduce the same error, before sending it over to PHP, don't try to make your own JSON string, use what JS offers you. Example:
var obj = { key: val, okey: oval }
objJSON = JSON.stringify(obj)
// objJSON is ALWAYS going to be valid json
Upvotes: 0