用于了解页面加载时的特定状态的常见 AJAX 约定是什么?
自从我开始做严肃的 ajax 工作以来,这一直是我一直有的一个问题。让我举个例子。
假设您从服务器中提取了客户的常规 HTML 页面。 URL 可能如下所示:
/myapp/customer/54
呈现页面后,您希望提供作用于该客户的 ajax 功能。为此,您需要在每个请求中将 ID“54”发送回服务器。
哪种方法是最好/最常见的?我发现自己把它放在隐藏的形式中。我觉得很容易选择,但也感觉有点脆弱。如果文档更改并且脚本不起作用怎么办?如果 3 个月后该 id 因 css 目的而重复,并因此破坏页面,因为有 2 个同名 id,该怎么办?
我可以解析 url 来获取值“54”。这种方法更好吗?它可以重复地处理简单的情况。对于您可能想要传递多个 id 或 id 列表的复杂情况,它可能不太适用。
我只是想知道一个最佳实践 - 一些健壮、干净、优雅并且值得称赞的东西。
This has been a question I've had since I started doing serious ajax stuff. Let me just give an example.
Let's say you pull a regular HTML page of a customer from the server. The url can look like this:
/myapp/customer/54
After the page is rendered, you want to provide ajax functionality that acts on this customer. In order to do this, you need to send the id "54" back to the server in each request.
Which is the best/most common way to do this? I find myself putting this in hidden form forms. I find it easy to select, but it also feels a bit fragile. What if the document changes and the script doesn't work? What if that id gets duplicated for css purposes 3 months from now, and thus breaks the page since there are 2 ids with the same name?
I could parse the url to get the value "54". Is that approach better? It would work for simple cases repeatedly. It might not work so well for complex cases where you might want to pass multiple ids, or lists of ids.
I'd just like to know a best practice - something robust that is clean, elegant and is given 2-thumbs up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为最好的方法就是像没有 Ajax 一样思考。
假设您有一个使用 Ajax 提交的表单。您如何知道将其发送到哪个 URL?
src 属性。 只需让您的脚本发送表单本身即可。所有数据都已在表格中。
假设您有一个加载一些新数据的链接。你怎么知道URL和参数?
href 属性。 只需让脚本读取 URL。
因此基本上您总是会从所操作的元素中读取 URL/数据,类似于浏览器的操作。
由于您的服务器端代码在加载页面时知道 ID 等,因此您可以轻松地在那里生成这些 URL。客户端代码只需要读取属性。
这种方法不仅仅有一个好处:
如果您正在做比链接/表单更复杂的事情
在需要允许更复杂的交互的情况下,您可以将 ID 或其他相关数据存储在属性中。 HTML5 为此提供了 data-* 属性 - 我建议您甚至使用这些如果您不使用 HTML5:
如果您的页面上有功能更齐全的应用程序,您也可以考虑简单地将您的 ID 存储在 JS 代码中。当您在 PHP 端生成标记时,只需在标记中包含一个片段,该片段将 ID 分配给变量或调用函数或您认为最好的任何内容。
I think the best way to do this is to think like you don't have Ajax.
Let's say you have a form which is submitted using Ajax. How do you know what URL to send it to?
The src attribute. Simply have your script send the form itself. All the data is in the form already.
Let's say you have a link which loads some new data. How do you know the URL and parameters?
The href attribute. Simply have the script read the URL.
So basically you would always read the URL/data from the element being acted upon, similar to what the browser does.
Since your server-side code knows the ID's etc. when the page is being loaded, you can easily generate these URLs there. The client-side code will only need to read the attributes.
This approach has more than just one benefit:
If you're doing something more complex than links/forms
In a case where you need to allow more complex interactions, you can store the IDs or other relevant data in attributes. HTML5 provides the data-* attributes for this purpose - I would suggest you use these even if you're not doing HTML5:
If you have a more full-featured application on the page, you could also consider simply storing your ID in JS code. When you generate the markup in the PHP end, simply include a snippet in the markup which assigns the ID to a variable or calls a function or whatever you decide is best.
理想情况下,您的表单应该在没有 JavaScript 的情况下工作,因此您可能有一个隐藏的表单输入或已经包含 id 值的内容。如果没有,你可能应该这样做。
从某种意义上说,这一切都是“脆弱的”,一个小小的变化就会影响一切,对此你无能为力,但你并不总是想通过读取 url 或查询字符串将其放在用户手中,这很容易由用户操纵。 (这当然适用于 url,但不适用于所有内容。适用于信任 $_GET 和查询字符串的相同规则也适用于此处)。
就我个人而言,我喜欢在现有的功能代码之上构建所有 AJAX,而且我从来没有遇到过“挂钩”已有代码的问题。
所有这些仍然是可能的,并且如果没有 javascript,并不完全困难,因此表单在任何一种情况下都可以工作,但我确实明白你在说什么。几乎在每种情况下,都有某种唯一的 id,无论是数据库 id 还是文件名,都可以用作表示它的 html 的“id”属性。 * 或 Jani Hartikainen 提到的
data-
属性。例如,我有一个模板系统,允许拖/放内容块。每个方块都有一个 ID,每个可以掉落的区域也有一个 ID。我将在包含的
div
id 上使用前缀,例如“template-area_35”或“content-block_264”。在这种情况下,我懒得回退而不使用 JavaScript,但它可以完成(例如下拉菜单->将其移动到区域)。无论如何,id 属性是一个很好的用途。如果发生这种情况(实际上不应该发生),则说明有人做错了事。如果代码无法运行,那是他们的错,他们要承担责任。根据定义,ID 应该是唯一。
Ideally your form should work without javascript, so you probably have a hidden form input or something that contains the id value already. If not, you probably should.
It's all "fragile" in the sense that a small change will affect everything, not much you can do about that, but you don't always want to put it in the user's hands by reading the url or query string, which can be easily manipulated by the user. (this is fine for urls of course, but not for everything. Same rules that apply to trusting $_GET and query strings apply here).
Personally, I like to build all AJAX on top of existing, functional code and I've never had a problem "hooking" into what is already there.
All of that is still possible, and not entirely difficult to do without javascript, so a form could work in either case, but I do indeed see what you're saying. In almost every case, there is some sort of unique id, whether it's a database id or file name, that can be used as the "id" attribute of the html that represents it. * Or the
data-
attribute as Jani Hartikainen has mentioned.For instance, I have a template system that allows drag/drop of blocks of content. Every block has an id and every area that it can get dropped has one as well. I will use prefixes on the containing
div
id like "template-area_35" or "content-block_264". In this case, I don't bother to fallback w/o javascript, but it could be done (dropdown-> move this to area for example). In any case, it's a good use of the id attribute.If that happens (which it really shouldn't), someone is doing something wrong. It would be their fault if the code failed to work, and they would be responsible. Ids are by definition supposed to be unique.
恕我直言,将其放在请求参数(即?customerId=54)中会很好,因为即使您无法处理 AJAX(例如在某些旧的移动浏览器、命令行浏览器等中),您仍然可以引用关联。
IMHO putting is at a request parameter (i. e. ?customerId=54) would be good 'cos even if you can't handle AJAX (like in some old mobile browsers, command-line browsers and so) you can still have a reference to the link.
显然,您有一个知道实体“Customer”的应用程序,您应该在 Javascript(或 PHP,但由于您正在执行 ajax,我会将其放入 Javascript)中反映这一点。
您可以将其包装到更多领域感知函数中,而不是手动进行每个 ajax 调用:
旧场景:
新场景:
将其四舍五入。是的,您需要为每个请求发送客户 ID,但我会将其包装在 Javascript 中的适当对象中。
Apparently you have an application that is aware of the entity "Customer", you should reflect this in your Javascript (or PHP, but since you're doing ajax I would put it in Javascript).
Instead of handmaking each ajax call you could wrap it into more domain aware functions:
Old scenario:
New scenario:
To round it up. Yes, you need to send the customer id for each request but I would wrap it in Javascript in proper objects.