<分区>
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)
休息
"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)
我真的对这些术语感到困惑,我用 Symfony2 编写网站代码并且一切正常,但是一旦我的老板问我我是怎么做到的,我真的不知道该用什么词来解释它。它可能是因为我把这一切作为一种爱好开始,并将我的一生都集中在实际的部分上。
假设我在客户端(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);
}
这在服务器端(带注释的 PHP/Symfony2 Controller )
/**
*@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));
}
}
}
我在这段代码的哪一部分使用了 REST? AJAX 是什么部分?我什至使用了 REST?