1

I've been reading some guides regarding REST in PHP and JavaScript. I'm facing a small implementation problem.

Assuming that I want to delete my server side to behave according to the received URI. for example:

If it receives on http DELETE URI: /users/563 it will delete user with the ID 546 My problem starts with the DELETE request on client side (JavaScript):

function ajaxRequest()
{

    var http_request = new XMLHttpRequest();
    http_request.onreadystatechange = function()
    {
        var done = 4, ok = 200;
        if (http_request.readyState == done && http_request.status == ok)
        {
             responseHandler(http_request.responseText);
        }
    };


    http_request.open("DELETE", php/dbHandler.php , true);
    http_request.send(data);
}

In the delete request I must specify the url of the server side in that case it is dbHandler.php Where should I insert the URI /users/563 without losing the server-side destination address?

On server side if I try to extract URI with the command $_SERVER['REQUEST_URI'] I will always get php/dbHandler.php what is the proper way to create/send the URI in client side and extract it in server-side?

Canttouchit
  • 3,149
  • 6
  • 38
  • 51
  • I’m not sure what’s going on. Surely you should be issuing the request to **users/563**, not **php/dbHandler.php**? – Martin Bean Jul 14 '13 at 19:19
  • Sounds like you should have `/users/563` as the URI, and setup some kind of URL rewriting on your server to have it handled by dbHandler.php. Also, see http://stackoverflow.com/questions/2539394/rest-http-delete-and-parameters – bfavaretto Jul 14 '13 at 19:20

2 Answers2

0

You need to work with some handler that knows how to translate your request according to your needs in the server side of course.

If you are using apache - it handles all the requests via .htaccess file

So in your case you want to translate this url http://www.domain.com/php/dbHandler.php with parameters to this url: http://www.domain.com/php/users/563 so your htaccess will look something like that:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^php/users/([0-9]+)/?$ php/dbHandler.php?uid=$1 [L]

This way the apache knows that if he gets url in the form php/users/123 he will redirect it to the actual url under php/dbHandler.php with param GET id value 123.

This is the basics - If you don't know it you should read about it here is stack you have tons of material.

Of course every server has it's own methods for redirect uris and let's say in microsoft IIS it's a bit different.

Adidi
  • 5,097
  • 4
  • 23
  • 30
0

I would have my request look like this:

function ajaxRequest(id){
 // your stuff here...
 http_request.open("DELETE", "users/" + id, true);
 // your other stuff ...
}

but you need to make sure you have a resource on the server side mapped to receive data at the URI /users/{id}

Joe Minichino
  • 2,793
  • 20
  • 20