关于API设计的问题

发布于 2024-10-31 08:15:51 字数 593 浏览 1 评论 0原文

我正在研究 Web API 的最佳“设计模式”。我目前使用 Django 作为我的 Web 框架。我创建了一个非 ajax 界面,该界面会导致整个页面在每次请求时重新加载。

现在,我开始将 ajax 合并到界面中。为了便于讨论,我需要添加 API 的两个示例功能如下:

1) beta 页面:用户提供电子邮件地址。我想对服务器端进行 ajax 调用,以查看它是否已存在于我的数据库中。我对此调用的最初设计是一个类似于 2) 个人资料图片上传的查看功能

def check_email(request):
    if request.method == "POST":
           # check db

           # return JSON true/false

新的个人资料图片会添加到您的页面,而无需重新加载整个页面

,据我所知, ,最好的方法是通过对视图函数的 POST 调用。然后,响应将返回 JSON,然后我可以将其相应地注入回 DOM 中。

有人可以让我知道我设计这个 API 的方向是否正确吗?

注意:我已经检查过 django-piston,它似乎也非常有用。

谢谢

I am in the process of researching the best "design-pattern" a web API. I am currently using Django as my web framework. I created a non-ajax interface that causes the entire page to reload at each request.

Now, I am starting incorporate ajax into the interface. For the sake of this discussion, two example pieces of functionality that I need to add an API for are the following

1) beta page: the user supplies an email address. i want to make an ajax call to the serverside to see if it already exists in my DB. my initial design for this call would be a view function similar to

def check_email(request):
    if request.method == "POST":
           # check db

           # return JSON true/false

2) profile picture uploads, where the new profile picture is added to your page without a full page reload

as far as i can tell, the best way to do this is via a POST call to a view function. then, the response would return JSON, which I can then inject back into the DOM accordingly.

can someone please let me know if I am on the right track with designing this API?

note: i have checked out django-piston, and it seems pretty useful also.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心病无药医 2024-11-07 08:15:51

是的,你似乎走在正确的道路上。一个狡辩:要检查数据库中是否存在电子邮件地址,理想情况下应该使用 GET 而不是 POST,因为没有信息被更新 - 您只是询问某些内容是否存在。

返回 JSON 的一种替代方法是返回预渲染的 HTML,您可以在适当的点将其直接注入到 DOM 中。这样做的优点是您可以使用 Django 的模板机制来渲染现有的模板片段 - 唯一的区别是您单独渲染片段,而不是在整个 HTML 页面中包含/扩展它。

Yes, you seem to be on the right track. One quibble: to check if an email address exists in the db, you should ideally use a GET rather than a POST, as no information is being updated - you are simply asking if something exists.

One alternative to returning JSON is to return pre-rendered HTML which you can inject directly into the DOM at the appropriate point. The advantage of doing it this way is that you can use Django's template mechanism to render your existing template fragments - the only difference is that you render a fragment in isolation, rather than including/extending it within an entire HTML page.

暖阳 2024-11-07 08:15:51

你是对的,但我会使用 request.is_ajax() 来检查它是否实际上是一个 ajax 请求以相应地返回响应。来自 django 请求文档

如果请求是通过 XMLHttpRequest 发出的,则通过检查 HTTP_X_REQUESTED_WITH 标头中的字符串“XMLHttpRequest”返回 True。大多数现代 JavaScript 库都会发送此标头。如果您编写自己的 XMLHttpRequest 调用(在浏览器端),并且希望 is_ajax() 正常工作,则必须手动设置此标头。

这将允许您为禁用了 javascript 的用户分隔返回值,从而使他们仍然可以正确使用您的网站。如果您不使用 javascript 库,则可以自行设置。

You're right on track, but I would use request.is_ajax() to check if it's actually a ajax request to return the response accordingly. from the django request docs

Returns True if the request was made via an XMLHttpRequest, by checking the HTTP_X_REQUESTED_WITH header for the string 'XMLHttpRequest'. Most modern JavaScript libraries send this header. If you write your own XMLHttpRequest call (on the browser side), you'll have to set this header manually if you want is_ajax() to work.

This will allow you to separate return values for users that have javascript disabled, allowing them to still use your site correctly. If you're not using a javascript library, you set this yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文