Need sample PHP web service for $.ajax

Hi,

I need a simple example of PHP webservice that will be consumed using jQuery.ajax.
If you have any references regarding it please share to me.

Thanks in advance.
Samuel

Hey Samuel,

I wrote something super basic for this once, here is a basic extract:


$json = "";

$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : "";
    
if ($action == "do_something") {                
    $data = do_some_action_that_returns_an_object();               
    
    //json encode your data        
    $json = json_encode($data);    
}

header("Content-type: application/json");    

print $json;

You just specify some parameters and then “do something” based on those parameters. You can then json_encode() your results and print them to have your data returned as a JSON result.

Please note that this is super super basic, and I haven’t used this for anything other than some personal things, but if you simply want to test ajax functionality then it will do the trick.

P.S. if you’re interested in testing AJAX functionality with jQuery, you might find that “Mockjax” will suffice: https://github.com/appendto/jquery-mockjax

Thank you.

It’s enough for me.