I'm developing a website where i have to communicate with web services. I’m using Ajax calls like this, for instance if I want to call a method called getCustomer(...) I have in my Zend project a module called "customer".
Inside of my controller, I have an action called "jsonAction"
public function jsonAction() {
$this->_helper->viewRenderer->setNoRender();
$server = new Zend_Json_Server();
$server->setClass('Customer_Model_Customer');
$server->handle();
exit;
}
Them in my model I have the function:
/**
* Get Customer
* @param string $customerNr
* @return object
*/
public function getCustomer($customerNr){
/*....*/
}
So I want to call this function using Ajax calls. What i did was something like this:
getCustomer : function(customerNr){
if(customerNr == null)
return;
var request = {};
request.method = "getCustomer";
request.params = {};
request.params.customerNr = customerNr;
request.id = Math.floor(Math.random()*101);
postObject(webServicesURL, JSON.stringify(request), successGetCustomer);
},
Where postObject it’s the Ajax function:
function postObject(url, request, successCallback){
try{
$.ajax({
type: "POST",
dataType: 'json',
url: url,
contentType: "application/x-www-form-urlencoded",
data: request,
xhrFields: {
withCredentials: true
},
success: function(data){
successCallback(data);
},
error: function (data) {
/*Notify error*/
}
});
} catch(ex){
/*erro*/
}
}
My question is, there is another way to do this? a best and elegant way? I'm new in web developing and that’s why i'm asking for your help.
Note: I'm using php Zend framework 1.12, Ember 1.0 and JQuery 1.8