简体   繁体   English

Backbone.js获取而不将数据作为URL字符串传递

[英]Backbone.js Fetch without passing data as URL string

Suppose I want to fetch data for a Backbone.js model, while passing additional data: 假设我要在传递其他数据的同时获取Backbone.js模型的数据:

myModel.fetch{ data: { bar: true } };

If you look at your request in Firebug, Backbone appended this data on to the URL: 如果您在Firebug中查看请求,Backbone会将以下数据附加到URL:

GET http://www.example.net/foo?bar=true

This data appears under the 'parameters' tab in Firebug. 此数据显示在Firebug的“参数”标签下。

However, if I call a post or put using Backbone, the extra data being posted is not sent in the URL, but appears under a 'PUT' tab in Firebug. 但是,如果我使用Backbone呼叫postput ,则发布的多余数据不会在URL中发送,而是显示在Firebug的“ PUT”标签下。

PUT http://www.example.net/foo //additional data is not included in URL

On my server side (i'm using Node.js > Express), I pull the passed params (example 1) with request.query , but I pull the passed data (example 2) with request.body . 在我的服务器端(我用的Node.js>快递),我拉过PARAMS(例如1) request.query ,但我拉传递的数据(例如2) request.body

My question: 我的问题:

Is it best practice to throw get params in the URL? 最好的做法是在URL中添加get参数吗? Should I have my server-side simply look for request.param for all GETs, or, is there a way to have Model.fetch() pass 'data' instead of 'params'? 我应该让服务器端只是为所有GET寻找request.param ,还是有办法让Model.fetch()传递“数据”而不是“参数”?

GET Method GET方法

The GET operation, as defined in HTML, put the query string (or parameters) in the URL, and this is sent to the server. HTML中定义的GET操作将查询字符串(或参数)放在URL中,然后将其发送到服务器。 This is done because the GET should be used to retrieve data only. 这样做是因为GET仅应用于检索数据。 This data can be cached since you have the full parameters in the URL, this URL can be bookmarked and can stay in the browser history. 由于您在URL中具有完整参数,因此可以缓存此数据,可以将该URL标记为书签并保留在浏览器历史记录中。

POST Method 开机自检方法

A POST operation is intended to submit data to the server. POST操作旨在将数据提交到服务器。 No cache is done in this kind of operation. 在这种操作中不会执行任何缓存。 This is not stored in the browser history. 这不会存储在浏览器历史记录中。 Since you don't want to expose what you are sending the content goes in the body. 由于您不想暴露正在发送的内容,因此内容就在体内。

Saying this, the behaviour of the methods make sense and you should use this boths methods properly in your application and also take care of them in the server side independently. 这样,方法的行为就很有意义了,您应该在应用程序中正确使用这两种方法,并在服务器端分别进行维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM