Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
user254883 Avatar asked Apr 14 '14 07:04

user254883


People also ask

Is AJAX and REST API are same?

Ajax means sending json or xml (but usually these days, json) outside of the initial HTML page load. A RESTFul API is one way to implement the structure of data you want to make public (also, usually, using json). and a POST is one of the ways to interact with that data, specifically the way you create new resources.

What is AJAX explain with example?

AJAX = Asynchronous JavaScript and XML. AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

What is the difference between AJAX request and HTTP request?

An AJAX request is just an HTTP request made by JavaScript code. Browsers load web pages by sending requests to the server using HTTP. Each time a page loads, a request is made. JavaScript code can do the same thing, but without needing to reload the whole page.

What is the difference between AJAX and fetch?

Fetch is compatible with all recent browsers including Edge, but not with Internet Explorer. Therefore, if you are looking for maximum compatibility, you will continue to use Ajax to update a web page. If you also want to interact with the server, the WebSocket object is also more appropriate than fetch.


Video Answer


1 Answers

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.

like image 82
Timotheus.Kampik Avatar answered Oct 19 '22 13:10

Timotheus.Kampik