I have php script with 5 function. The script gets "request_type" "user_id" and "youtube_id" and execute a function (one of a five) suitable to the "request_type"
Each function returns me json.
Now on the client side i have this javascript function. I want it to be generic as much as possible, Which mean that i want it to get data from php and return it as object.
function request_to_server(f_request_type, f_user_id, f_youtube_id) {
var my_answer;
$.ajax({
type : "POST",
dataType : "json",
url : "http://example.com/youtube_save.php",
data : {request_type: f_request_type, user_id : f_user_id, youtube_id:f_youtube_id },
success: function(response) {
my_answer = JSON.parse( response );
}
});
return my_answer;
}
Here is the example of another function that get this object
function show_button_or_not() {
var user_id = $('#hold_user_id').val();
var answer = request_to_server('show_button','89',"lala");
if(answer.success) {
alert("yes");
} else {
alert("no");
}
}
But as i said, the php can return different kinds of json. It can have (optional) response.success, response.book, response.author.
Anyone can explain how to do that?