网红快餐店

by Kevin Kononenko

凯文·科诺年科(Kevin Kononenko)

在一家快餐店工作解释了AJAX基础知识 (AJAX Basics Explained By Working At A Fast Food Restaurant)

AJAX (Asynchronous JavaScript And XML) can be pretty confusing if you do not have a firm understanding of server-side code.

如果您对服务器端代码没有足够的了解,那么AJAX(异步JavaScript和XML)可能会令人困惑。

When I started with web development, I first learned HTML, CSS, JavaScript, and jQuery before I ventured into Node.js and Ruby on Rails.

当我开始Web开发时,我首先学习HTML,CSS,JavaScript和jQuery,然后才涉足Node.js和Ruby on Rails。

But, of course, I wanted to understand how to build dynamic web applications, so I needed to learn how to use AJAX to communicate with a server. I didn’t want to just build static pages that were straight out of 2005.

但是,当然,我想了解如何构建动态Web应用程序,因此我需要学习如何使用AJAX与服务器进行通信。 我不想只构建2005年以来的静态页面。

The front-end is a completely different challenge than back-end. I struggled to understand the different parts of a GET or POST request.

前端与后端是完全不同的挑战。 我很难理解GET或POST请求的不同部分。

So, I came up with the analogy of a fast-food restaurant to explain it. If you have been to a McDonald’s, Burger King or Wendy’s, then you can write your own GET and POST requests.

因此,我想出了一家快餐店的类比来解释它。 如果您去过麦当劳,汉堡王或温迪,则可以编写自己的GET和POST请求。

In order to understand this post, you will need to have a beginner’s understanding of jQuery.

为了理解本文,您需要对jQuery有一个初学者的了解。

AJAX是什么样的? (What does AJAX look like?)

Have you ever noticed that you can comment on a post on Facebook without reloading the entire page? That is AJAX at work. AJAX allows users to interact with your web application without completely reloading the page.

您是否曾经注意到您可以在Facebook上发表评论而无需重新加载整个页面? 那就是AJAX在工作。 AJAX允许用户与您的Web应用程序进行交互,而无需完全重新加载页面。

Imagine if every time you “liked” a post on Facebook or added a comment, the page reloaded? That would be terrible! Instead, Facebook quickly adds your ‘comment’ or ‘like’ to the post and allows you to keep reading. They add that interaction to their database without interrupting your experience!

想像一下,如果您每次“喜欢” Facebook上的帖子或添加评论,页面是否都会重新加载? 那太可怕了! 相反,Facebook会在帖子中快速添加您的“评论”或“喜欢”,并允许您继续阅读。 他们在不中断您的体验的情况下将该交互添加到其数据库中!

为什么我们需要AJAX? (Why do we need AJAX?)

Okay, those are anecdotal examples, so let’s look at the entire system.

好的,这些都是轶事示例,因此让我们看一下整个系统。

Think of your whole web application as a fast-food restaurant. You are the cashier, the person on the front-lines. You handle requests from customers.

将整个Web应用程序视为一家快餐店。 您是收银员,一线人员。 您处理来自客户的请求

If you look at this diagram, I can see three separate jobs that need to be done.

如果您查看此图,我可以看到需要完成的三个单独的作业。

  1. The cashier must handle user requests at a fast pace.收银员必须快速处理用户请求。
  2. You need cooks to throw the burgers on the grill and cook all the food.您需要厨师将汉堡包放在烤架上,然后烹饪所有食物。
  3. You need a meal prep team to package the food up and put it in a bag or on a tray.您需要一个备餐团队来包装食物并将其放在袋子或托盘中。

However, if you did not have AJAX, you would only be allowed to process one order at a time from start to finish! You would need to take the order… then charge the customer… then sit there doing nothing while people in the kitchen cook the food…. then continue waiting while the meal prep team packages it up. You could only take the next order after all that.

但是,如果您没有AJAX,则只能从头到尾处理一次订单! 您需要下订单...然后向客户收费...然后坐在那里什么都不做,而厨房里的人正在煮食物...。 然后在准备食物的团队打包之前继续等待。 之后,您只能接受下一个订单。

Now THAT is a bad user experience! You would not be able to call it “fast food” anymore. Instead, you would need to call it “mediocre food”… or something.

现在,这是糟糕的用户体验! 您将无法再将其称为“快餐”。 取而代之的是,您需要将其称为“中餐”……。

AJAX allows for an asynchronous processing model. That means you can request data or send data without loading the entire page. This is just like the way a normal fast food restaurant operates. As the cashier, you take the customer’s order, send it over to the kitchen team, and get ready to take the next customer’s order.

AJAX允许使用异步处理模型 。 这意味着您可以在不加载整个页面的情况下请求数据或发送数据。 就像普通快餐店的经营方式一样。 作为收银员,您要接受客户的订单,将其发送给厨房团队,并准备接受下一个客户的订单。

Customers can continue to make orders, and you do not need to sit there while the employees in the kitchen work and make everybody wait.

客户可以继续下订单,而您不必在厨房的员工坐在那里工作时让所有人都在等待。

This certainly introduces some complexity. You now have multiple specializations within the restaurant. Additionally orders are being handled at different paces. But, it creates a much better user experience.

这无疑会带来一些复杂性。 您现在在餐厅内拥有多个专业。 另外,订单的处理速度也不同。 但是,它创建了更好的用户体验。

You have probably seen this in action at a restaurant yourself. One person is working the fries machine. One person is managing the grill. When an order comes in, the cashier can instantly communicate with both and get back to taking orders.

您可能自己已经在一家餐厅看到了这种效果。 一个人正在炸薯条机。 一个人正在管理烧烤架。 接到订单后,出纳员可以立即与两者进行通讯,并恢复接受订单。

如何创建POST请求 (How To Create A POST Request)

Let’s put these concepts to work. As the cashier, you need to send incoming customer requests to the kitchen so that the rest of your team can prepare the meal. You can do that with POST request.

让我们将这些概念付诸实践。 作为收银员,您需要将传入的客户请求发送到厨房,以便团队的其他成员可以准备餐点。 您可以使用POST请求执行此操作。

In your actual code, a POST request sends data to your server. That means that you are sending order data to the back-end, in this case.

在您的实际代码中,POST请求将数据发送到您的服务器。 这意味着在这种情况下,您正在将订单数据发送到后端。

It has three major parts:

它包含三个主要部分:

  1. A URL: this is the route that the request will follow. More in a minute.

    URL :这是请求将遵循的路由。 一分钟内会更多。

  2. Data: any extra parameters that you need to send to the server.

    数据 :您需要发送到服务器的任何其他参数。

  3. Callback: What happens after you have sent the request

    回调 :发送请求后会发生什么

What are some common things that people order in a fast-food restaurant? Let’s look at 2 examples.

人们在快餐店点菜有哪些常识? 让我们看两个例子。

  1. Fries薯条
  2. A combo meal composed of a burger, fries and a drink汉堡,薯条和饮料组成的套餐

These two require different processes. A fries request might only need one person to scoop some fries into a sleeve. But a combo meal order will require work from multiple team members. So, these two need different URLs.

这两个需要不同的过程。 一份薯条请求可能只需要一个人即可将一些薯条oop到袖子中。 但是一份组合餐将需要多个团队成员的工作。 因此,这两个需要不同的URL。

$.post('/comboMeal')
$.post('/fries')

The URL allows us to use the same logic on the back-end for certain types of requests. That part is outside of the scope of this tutorial, so you can dig into that a little more when you look at the back-end.

该URL允许我们对某些类型的请求在后端使用相同的逻辑。 该部分不在本教程的讨论范围之内,因此在查看后端时,您可以对其进行更多的研究。

Next is the data. This is an object that tells us a little bit more about the request. For the combo meal URL, we probably need to know:

接下来是数据 。 这个对象告诉我们有关请求的更多信息。 对于组合餐URL,我们可能需要知道:

  1. The type of main meal主餐类型
  2. They type of drink他们类型的饮料
  3. The price价格
  4. Any special requests任何特殊要求

For the fries, we might only need to know:

对于薯条,我们可能只需要知道:

  1. The size of the fries薯条的大小
  2. The price价格

Let’s look at an example of a combo meal: a cheeseburger with a Pepsi that costs $6.00. Here is what that looks like in JavaScript.

让我们看一个组合餐的示例:百事可乐的芝士汉堡售价6.00美元。 这就是JavaScript中的样子。

let order = {  mainMeal: 'cheeseburger',  drink: 'Pepsi',  price: 6,   exceptions: '' };
$.post('/comboMeal', order);

The order variable holds the contents of the order. And then we include it in the POST request so that our kitchen staff knows what the heck to put in the combo meal!

order变量保存订单的内容。 然后,我们将其包含在POST请求中,以便我们的厨房工作人员知道在组合餐中放些什么!

But, we can’t have all of this code run at a random time! We need a trigger event that will set off the request. In this case, a customer order at a fast food restaurant is kind of like a person that clicks an ‘order’ button on your website. We can use jQuery’s click() event to run the POST when the user clicks a button.

但是,我们不能让所有这些代码都随机运行! 我们需要一个触发事件来触发请求。 在这种情况下,快餐店的客户订单就像单击网站上“订单”按钮的人一样。 当用户单击按钮时,我们可以使用jQuery的click()事件运行POST。

$('button').click(function(){   let order = {     mainMeal: 'cheeseburger',    drink: 'Pepsi',     price: 6,     exceptions: ''   };   $.post('/comboMeal', order); });

Last part. We need to tell the customer something after their order has been sent. Cashiers usually say “Next customer please!” since this is a fast food restaurant, so we can use that within the callback to show that the order has been submitted.

最后部分。 在他们的订单发送后,我们需要告诉客户一些事情。 收银员通常会说“请下一位顾客!” 由于这是一家快餐店,因此我们可以在回调中使用它来显示已提交订单。

$('button').click(function(){    let order = {     mainMeal: 'cheeseburger',     drink: 'Pepsi',     price: 6,     exceptions: ''    };
$.post('/comboMeal', order, function(){     alert('Next customer please!');   }); })

如何创建GET请求 (How To Create A GET Request)

So far, we have the ability to submit an order. Now, we need a way to deliver that order to our customer.

到目前为止,我们已经能够提交订单。 现在,我们需要一种将订单交付给客户的方法。

This is where GET requests come in. GET allows us to request data from the server (or kitchen, this analogy). Please note: right now, our database is full of orders, not the food itself. This is an important distinction because GET requests do not change our database. They only deliver that information to the front-end. POST requests change the information in the database.

这就是GET请求的来源。GET允许我们从服务器(或类似的厨房)中请求数据。 请注意:现在,我们的数据库里满是订单,而不是食物本身。 这是一个重要的区别,因为GET请求不会更改我们的数据库 。 他们仅将这些信息传递到前端。 POST请求更改数据库中的信息。

Here are some typical questions you might get asked before receiving your food.

这是您在领取食物之前可能会被问到的一些典型问题。

  1. Would you like to eat here or get the food to go?您想在这里吃饭还是带走食物?
  2. Do you need any condiments (like ketchup or mustard)?您需要任何调味品(例如番茄酱或芥末酱)吗?
  3. What is your number on the receipt (to verify it is your food)?您在收据上的号码是多少(以确认它是您的食物)?

So, let’s say you ordered three combo meals for your family. You want to eat the food in the restaurant. You need ketchup. And the number on your receipt is 191.

因此,假设您为家人订购了三顿套餐。 您想在餐厅里吃饭。 你需要番茄酱。 收据上的号码是191。

We can create a GET request with a URL of ‘/comboMeal’, which corresponds to the POST request along with the same URL. However, this time we need different data. It is a totally different type of request. The same URL name just allows us to better organize our code.

我们可以创建一个URL为“ / comboMeal”的GET请求,该请求对应于POST请求以及相同的URL。 但是,这次我们需要不同的数据。 这是完全不同的请求类型。 相同的URL名称仅使我们可以更好地组织代码。

let meal = {  location: 'here',  condiments: 'ketchup',  receiptID: 191 };
$.get('/comboMeal', meal);

We also need a trigger for this one. This request is triggered by customers answering your questions as the cashier before you deliver the food to them. There is no convenient way to represent questions and answers with JavaScript. So I will just create another click event for the button with class ‘answer’.

我们还需要一个触发器。 客户在将食物交付给他们之前,以收银员的身份回答您的问题,从而触发了此请求。 没有方便的方法来表示JavaScript的问题和答案。 因此,我将为类别为“ answer”的按钮创建另一个click事件。

$('.answer').click(function(){  let meal = {     location: 'here',     condiments: 'ketchup',     idNumber: 191,   };
$.get('/comboMeal', meal); });

This one also needs a callback function, because we are going to receive whatever was contained in the three combo meals in order 191. We can receive that data through a data parameter in our callback.

这个还需要一个回调函数,因为我们将按191的顺序接收三个组合餐中包含的任何东西。我们可以通过回调中的data参数接收该数据。

This will return whatever the back-end stipulates for order 191. I am going to use a function named eat to signify that you eventually get to eat the food, but keep in mind that there is no eat function in JavaScript!

这将返回后端为订单191规定的内容。我将使用一个名为eat的函数来表示您最终可以吃到食物了,但是请记住,JavaScript中没有eat函数!

$('.answer').click(function(){   let meal = {     location: 'here',     condiments: 'ketchup',     idNumber: 191,    };   //data contains the data from the server   $.get('/comboMeal', meal, function(data){      //eat is a made-up function but you get the point      eat(data);   }); });

The final product, data, would contain the contents of the three combo meals, theoretically. It depends on how it is written on the back-end!

理论上,最终产品data将包含这三种组合餐的内容。 这取决于它在后端的编写方式!

尝试其他视觉解释 (Try Other Visual Explanations)

Did you enjoy this tutorial? Give it a clap so others can see it! Or, sign up for the newsletter to hear about the latest releases of CSS and JavaScript tutorials.

您喜欢本教程吗? 给它鼓掌,以便其他人可以看到它! 或者, 注册以获取有关CSS和JavaScript教程的最新版本的新闻通讯

翻译自: https://www.freecodecamp.org/news/ajax-basics-explained-by-working-at-a-fast-food-restaurant-88d95f5fcb7a/

网红快餐店

网红快餐店_在一家快餐店工作解释了AJAX基础知识相关推荐

  1. vrp 节约算法 c++_数据结构和算法(Golang实现)(8.1)基础知识-前言

    基础知识 学习数据结构和算法.我们要知道一些基础的知识. 一.什么是算法 算法(英文algorithm)这个词在中文里面博大精深,表示算账的方法,也可以表示运筹帷幄的计谋等.在计算机科技里,它表示什么 ...

  2. seo技术_基础知识_网站pr值的意义_日思663.带你入门SEO基础知识

    2019/9/6 这篇文章来自36氪产品团队一次内部分享,按照惯例记录下来,也分享给大家~ 本文937字,阅读约9分钟 从上上周开始,产品团队每周都会请一位小伙伴给大家做分享,主题不限,以自己擅长或感 ...

  3. 5不触发系统键盘_智能化子系统红外周界报警的基础知识介绍!

    防盗报警系统是智能化系统必不可少的子系统,今天就系统介绍一下防盗报警系统基础知识,适合新手学习参考. 首先看两张防盗报警系统图: 此图为周界红外报警系统图 此图为电子围栏系统图. 那么下面就来简单的说 ...

  4. python 图像分析自然纹理方向与粗细代码_数字图像处理与Python实现笔记之基础知识...

    数字图像处理与Python实现笔记之基础知识 摘要 绪论 1 数字图像处理基础知识 1.1 数字图像简介 1.1.1 数字图像处理的目的 1.1.2 数字图像处理的应用 1.1.3 数字图像处理的特点 ...

  5. 森林图怎么分析_股票趋势图怎么看 这些炒股基础知识建议掌握-趋势分析-股票技巧-股票入门基础知识学习网...

    股票看趋势是投资者一贯的方法,其实这是有一定道理的.因为,趋势具有惯性,当出现向下趋势时,这将会发生向下发展的,而且这种惯性将不会在短时间内发生逆转.那么,股票趋势图怎么看呢?下面小编就带大家来简单的 ...

  6. 互联网工作 常用名词及基础知识扫盲

    点击上方"逆锋起笔",公众号回复 pdf 领取大佬们推荐的学习资料 运营类 1.效果数据名词 UV: Unique Vister,独立访客. PV:page view,即网站被浏览 ...

  7. java 并发编程多线程_多线程(一)java并发编程基础知识

    线程的应用 如何应用多线程 在 Java 中,有多种方式来实现多线程.继承 Thread 类.实现 Runnable 接口.使用 ExecutorService.Callable.Future 实现带 ...

  8. 正交变换在基下的矩阵都是可逆阵_矩阵分析与应用(一,矩阵基础知识)

    前言:花了一个半月时间学习了 北大丘维声的<高等代数>.北理史荣昌的<矩阵分析>.清华张贤达的<矩阵分析与应用>:北大与哈工大的网课. 本质:(万物皆矩阵)矩阵论主 ...

  9. 浮栅场效应管 符号_学修手机要了解维修的基础知识场效应管:指南舟手机维修培训学校...

    前面我们讲了三极管和二极管这个大家基本上都是耳熟能详,那么我们这一期讲场效应管.那么场效应管和二极管和三极管的样子有什么区别呢. 场效应管(Field Effect Transistor,简称FET) ...

最新文章

  1. php umount强制,linux mount挂载与umount 卸载及“Device is busy”问题 | 璞玉(POOY)
  2. MSP430G2553需要注意的一些参数
  3. 安软件一劳永逸_如何克服一劳永逸地公开演讲的恐惧
  4. EHcache缓存框架详解
  5. php打印订单,WooCommerce: 打印订单
  6. sdut 1500 Message Flood(Trie树)
  7. java对公项目_5个让人激动的Java项目
  8. depthmap资料总结
  9. ARM base instruction -- lsl asl lsr asr ror rrx
  10. CAD图纸可以约束转换后图纸格式的大小吗?
  11. 年鉴表格-数据可视化分析
  12. 七.项目管理基础知识
  13. PMON分析(1)- ROM阶段
  14. 树的计数 Prufer序列+Cayley公式
  15. 6 种常用的项目管理模式
  16. Flutter Sliver滚动组件
  17. Ubuntu美化开关机界面 - Linux
  18. Python脚本实现淘宝秒杀
  19. 助推建筑业数字化转型升级,紫光云再出招
  20. 折叠目录html,在WEB页中实现折叠式动态目录结构

热门文章

  1. 扫描服务器端口信息工具,服务器端口扫描工具
  2. 小程序获取用户所在城市完整代码
  3. 微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能
  4. unity3d做简单小游戏可以吗?
  5. Docker1.12让容器使用和宿主机同一个网段
  6. CentOS 7更新时出现Multilib version problems
  7. Ovirt 安装部署方法
  8. iOS开发之圆角指定
  9. 趣味图形之 余弦函数cos与直线相交(另一种相交)
  10. iOS 设置UILabel 的内边距