dongliao9233 2013-12-29 10:27
浏览 31
已采纳

这个HTTP请求如何工作?

To begin, this question deals primarily with HTTP requests, BackboneJS, some sort of RESTful API (such as Slim API), and how these things work with each other. Additionally, this question is coming from someone who doesn't have much experience on the server-side, other than just handling basic PHP/MySQL stuff.

I've been looking at Backbone, and I've seen some tutorials regarding the use of RESTful APIs on the back-end (including this one from 9bit).

I also read this answer to a StackOverflow question (Understand BackboneJS REST Calls).

If I open up a JS file, and type type in some code to send a POST request such as this:

(function() {
var http = new XMLHttpRequest();

var value = '{ "prop1": "value 1", "prop2": "value 2" }';

http.open('POST', 'dir', true);
http.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
http.setRequestHeader('Content-Length', value.length);
http.onreadystatechange = function () {
    if (http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(value); 
})();

I see in the console that it sent a POST request looking something like this:

Method: POST
Body: { "prop1": "value 1", "prop2": "value 2" }
Location: http://localhost/~myusername/Todo/dir/

And then, since it's just my local server, the server sends back a response with the basic HTML page that shows the files in that directory, etc.

I tried using a GET request to retrieve a similar JSON object as well, but obviously I couldn't get anything from that location, presumably because the object I was trying to request from some empty folder doesn't even exist anywhere.

My question is, when you use a BackboneJS method such as .save(), from what I understand, it might use, in that instance, a PUT method to send a request with a body of an object, perhaps parsed as a string, to a directory, such as 'article/id', with 'id' possibly being something like '43' (potentially the corresponding id of whatever model's properties you sent). So...

1) What does an API, such as Slim do with that request?
2) Where is it saving those object properties to (a MySQL database)?
3) Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database? (I'm wondering why the location is simply a 'folder', such as '/article'. To put it in another context, whenever you type in a website like 'http://www.mywebsite.com', the server will automatically look for an 'index' page in that directory, such as 'index.html', and automatically open that file as the default file of that directory. Is that same type of thing happening in the context of using a location '/somefoldername' as the location of the HTTP request)?

Basically, it just looks strange to me that you would send an HTTP request to just some folder, and not a specific PHP file (for example) that would handle the request and communicate with a database. Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason?

Thank you very much.

  • 写回答

1条回答 默认 最新

  • douchao5864 2013-12-29 11:12
    关注

    Since you asked your questions in the context of Slim, I'm going to answer them that way, although much of the information will generally apply to other web applications/frameworks.

    What does an API, such as Slim do with that request?

    Not to be to cheeky, but it does whatever you (or the API developer) wants it to do (more on that shortly).

    Where is it saving those object properties to (a MySQL database)?

    In general, those object properties are being used to create a resource (POST) or to updated a resource (PUT), and most likely the resource is being persisted in some sort of storage, be it an RDMS or a NoSQL solution.

    Is there a file, such as 'index.php', sitting in the directory 'article', in which a script grabs the parameters in the body of the POST requests and uses though to communicate with the MySQL database?

    Here's where things get interesting, IMHO. Considering the route article/id, here's what happens in a Slim application:

    1. A request is received at example.com/article/22
    2. The request is routed to a front controller script which, based on the request URI and HTTP method, makes a decision about what to do with the request.
    3. If a PUT route for article exists, then the application code perhaps would take the request body and update the resource identified by the provided id.

    With that in mind, in a Slim application, likely the only web accessible file is index.php. What appear to be directories are merely routes defined in the application's index.php that Slim used to make decisions about how to handle requests. With that in mind . . .

    Using BackboneJS with a RESTful API, would our theoretical folder '/article' even exist, or is that just appended to the URL for some reason?

    In the context of a Slim application, no, /article wouldn't exist as a directory, but rather a route.

    Perhaps this won't help much, but this is what a portion of that index.php routing file might look like on the Slim side:

    $app->post('/article', function () {
        // Get data from post
        // Create resource
    });
    
    $app->get('/article/:id', function ($id) {
        // Return an article resource identified by $id
    });
    
    $app->put('/article/:id', function ($id) {
        // Use $id to retrieve resource from storage
        // Update resource with request data
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?