Can someone explain the difference between Ajax and rest? [closed]
AJAX
Asynchronous Javascript and XML”. Ajax loosely defines a set of
technologies to help make web applications present a richer user
experience. Data updating and refreshing of the screen is done
asynchronously using javascript and xml (or json or just a normal http POST)
REST
“Representational State Transfer”. Applications using REST principles
have a Url structure and a request/response pattern that revolve
around the use of resources. In a pure model, the HTTP Verbs Get,
Post, Put and Delete are used to retrieve, create, update and delete
resources respectively. Put and Delete are often not used, leaving Get
and Post to map to select (GET) and create, update and delete (POST)
I’m really confused about these terms, I code websites with Symfony2 and everything always works, but as soon as my boss asks me how I did it I don’t really know the words to use to explain it.It might be because I started all this as a hobby and spent my life concentrating on the practical parts.
Lets say I have this code on the client side (javascript):
function image_remover(myimageId,path)
{
// creating xmlhttprequest using ajax
var xml = ( window.XMLHttpRequest ) ?
new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
xml.open("GET", path+"?imageId="+myimageId, true);
xml.setRequestHeader("Content-type", "application/json");
xml.onreadystatechange = function()
{
if( xml.readyState === 4 &&
xml.status === 200 )
{
var serverResponse = JSON.parse(xml.responseText);
switch(serverResponse.d)
{
// do stuff
}
}
}
xml.send(null);
}
And this on the server side (PHP / Symfony2 Controller with annotations)
/**
*@Route("/removeImage",name="image_remover")
*/
public function removeImageAction(Request $request)
{
//If user is not logged in..
if (false === $this->get('security.context')->isGranted('ROLE_USER'))
{
//ip block
return new Response("an error has occured");
}
//My requests
$current_imageId = intval($request->query->get('imageId'));
//Getting image repository
$em = $this->getDoctrine()->getManager();
$db_myimage = $em->getRepository('GabrielUploadBundle:Image')->findOneById($current_imageId);
//if image was found
if($db_myimage)
{
//Owner of this image
$imageowner = $db_myimage->getImageowner();
//Getting user name
$user = $this->getUser();
$current_username = $user->getUsername();
// is username == imageowner? if not = block ip
if($current_username == $imageowner)
{
//remove image from database
$em->remove($db_myimage);
$em->flush();
// d = deleted y = yes
$response = array("d"=>1);
return new Response(json_encode($response));
}
else
{
//ip block
$response = array("d"=>0);
return new Response(json_encode($response));
}
}
else
{
//image object not found
//d = deleted, n = not found
$response = array("d"=>0);
return new Response(json_encode($response));
}
}
}
At what part of this code did I use REST? What part is AJAX? did I even use REST?
I will not comment on your code in detail, but:
AJAX basically refers to making asynchronous request in JavaScript, traditionally sending/receiving XML (although nowadays, JSON is often used instead of XML).
So that’s the technique you use on client-side.
REST is a concept for HTTP request exchange, so you’re making RESTful request calls (e.g. ‘get’) against the REST-API you implemented on server side.
See: Is AJAX a Rest api
And you might want read a little bit up about REST and AJAX on Wikipedia and other easy-to-access information sources.