这个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.

douluan1533
douluan1533 一个很好的例子就是这个localtodos.com,看看它的作用
接近 7 年之前 回复
drc4925
drc4925 此外,如果你看看这个答案,同样的事情正在发生......对我来说,看起来他们正在将对象保存为/user/12345。
接近 7 年之前 回复
douduan5073
douduan5073 它是一个通过Web浏览器查看的目录,但不是在目录中,在物理上我可以从backbone.js的文档中读取
接近 7 年之前 回复
duanguo7021
duanguo7021 我不明白。mywebsite.com/tasks/列表的位置如何,例如,等待进程的任务?/tasks/不是一个目录?
接近 7 年之前 回复
doukanhua0752
doukanhua0752 /被称为列表方式,例如/tasks/将返回所有等待进程的任务,它不是一个真正的目录列表,你看到的是php文件吗?或以“目录”的形式存储的项目?
接近 7 年之前 回复
dopgl80062
dopgl80062 在我的问题的第一部分中列出的链接中。此外,如果您在BackboneJS上进行谷歌搜索以及它如何与服务器上的RESTfulAPI一起工作,那么网络上有很多教程。
接近 7 年之前 回复
dongzhenqi2015
dongzhenqi2015 请求dir而不是文件对我来说没有意义,你在哪里看到(文档)你需要这样做?
接近 7 年之前 回复
doumisha5081
doumisha5081 是的,如果我向空目录发送请求,我希望得到一个目录列表。我的问题很简单,为什么当使用Backbone和RESTfulAPI时,你会只向一个目录发送请求而不是脚本文件(如PHP文件)?这里有什么我想念的吗?
接近 7 年之前 回复
dsff788655567
dsff788655567 是的,我安装了PHP等。我的AMP堆栈工作正常,例如我发送一个POST/GET请求到一个文件(比如'add_to_db.php'),它与我的MySQL数据库通信。在这方面没有问题。
接近 7 年之前 回复
douxian1939
douxian1939 听起来你得到的是目录列表而不是实际被调用的代码,是吗?你配置你的服务器来处理PHP文件?
接近 7 年之前 回复

1个回答

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
});
doubi8965
doubi8965 很好的答案,感谢您的示例和支持链接。
接近 7 年之前 回复
douqiao1887
douqiao1887 另见Q和A:stackoverflow.com/questions/9816274 / ... 很多好的,相关的Backbone.js信息和答案都使用了Slim。
接近 7 年之前 回复
dpn517111
dpn517111 答对了。 “路由”位是API如何响应对“/ article”发出的请求,而应用程序中不存在“/article/index.php”。 看一个实例可能会有所帮助。 这个index.php(github.com/jeremykendall/flaming-archer/blob/develop/public/...)是这个应用程序的前端控制器(365.jeremykendall.net)。 照片端点的POST和DELETE路由可能特别相关。
接近 7 年之前 回复
dongpin1059
dongpin1059 在这种情况下,这会被称为路由吗?
接近 7 年之前 回复
doubaoxue5788
doubaoxue5788 好的,我点击主页面中的链接(index.php)。 该链接发送一个URI请求/ article / 43的POST请求。 index.php中的脚本(利用Slim的API)告诉浏览器,如果它看到URI / article / 43,它应该执行一个回调函数,可能部分请求的资源是函数的参数。 理论上,回调函数可以从NoSQL数据库等返回JSON对象。这有点在正确的轨道上吗?
接近 7 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐