当前位置: 首页 > 编程日记 > 正文

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

网红快餐店

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/

网红快餐店

相关文章:

ThinkPHP 3.2 中获取所有函数方法名,以及注释,完整可运行

<?phpnamespace Home\Controller;use Common\Controller\BaseController;class AuthController extends BaseController{/*** cc index主页面*/public function index(){$modules array(Home); //模块名称$i 0;foreach ($modules as $module) {$all_controller $this-…

减少Building 'Xxx' Gradle project info等待时间

转载请注明出处&#xff1a;http://www.cnblogs.com/cnwutianhao/p/6640279.html 从Github上看到好的Demo想要Download下来学习。导入到Android Stduio的时候经常会碰到这样的事情。。。 等了半天没反应还是这个界面&#xff0c;老子要报警了&#xff01;&#xff01;&#xf…

js表单验证,如果不为空时自动改变提交按钮的背景色

<!DOCTYPE html><head><title>js验证表单,如果表单都不为空,则按钮颜色自变,某为空时恢复原本背景色</title><script language"javascript" type"text/javascript">var a ;var b ;function chadnge(e) {a e;if(a ! &…

ux可以去哪些公司_忽略UX会如何伤害您的API以及您可以如何做

ux可以去哪些公司by Ifeoluwa Arowosegbe通过Ifeoluwa Arowosegbe 忽略UX会如何伤害您的API以及您可以如何做 (How ignoring UX hurts your API and what you can do about it) Creating experiences that users love is crucial to the success of any product. Companies in…

初识java类的接口实现

初识java类的接口实现 如果两个类之间不存在继承关系&#xff0c;且两个类都想实现同一个接口&#xff0c;两个类都必须实现接口中全部方法&#xff0c;否则报语法错误如果两个类之间存在继承关系也想实现同一个接口&#xff0c;父类如果实现了某个接口的全部方法&#xff0c;从…

KMP的next[]数组

KMP是众多字符串问题的基础 理解next数组尤为重要 next又称前缀数组 是 它所处位置的前一个位置的元素 与 该链 链首开始 几个元素相匹配(即相同) 举个实例来说明&#xff1a; next对应的是该位置的前一个元素&#xff0c; 即next[i]对应a[i-1] 因为-1头指针的存在 next均对应…

[微信小程序]手指触摸动画效果(完整代码附效果图)

微信小程序开发交流qq群 173683895 本文共有两个示例,先上图 示例一: 示例二: 示例一代码(微信小程序): // pages/test/test.js Page({containerTap: function (res) {var that thisvar x res.touches[0].pageX;var y res.touches[0].pageY 85;this.setData({rippleS…

webpack 占位符_通过示例学习Webpack:占位符图像模糊

webpack 占位符by Kalalau Cantrell通过Kalalau Cantrell 通过示例了解Webpack&#xff1a;占位符图像模糊 (Learn Webpack by Example: Blurred placeholder images) The repo that goes along with this post uses webpack 3. If you are interested in learning webpack 4,…

UIImage 各种处理(分类)

1 interface UIImage (conversion)2 3 //压缩图片到宽度10244 (UIImage *)imageCompressSizeToMin1024Width:(UIImage *)image;5 6 7 //修改图片size8 (UIImage *)image:(UIImage *)image byScalingToSize:(CGSize)targetSize;9 10 //UIColor 转UIImage 11 (UIImage *)…

Matlab随笔之矩阵入门知识

直接输入法创建矩阵 – 矩阵的所有元素必须放在方括号“[ ]”内&#xff1b; – 矩阵列元素之间必须用逗号“&#xff0c;”或空格隔开&#xff0c;每行必须用“;”隔开 – 矩阵元素可以是任何不含未定义变量的表达式。可以是实数&#xff0c;或者是复数。 – 例a[1,2;3,4] 或 …

[微信小程序]提交表单返回成功后自动清空表单的值

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 实现思路: 给每一个input绑定相同的value对象,提交成功后把这个对象赋值为空. 下面看代码: <form bindsubmitformsubmit><view><span>* </span>公司名称:&l…

xebium周末启动_我如何在周末建立和启动聊天机器人

xebium周末启动by Mike Williams由Mike Williams 我如何在周末建立和启动聊天机器人 (How I Built And Launched A Chatbot Over The Weekend) 在数小时内将您的想法带入功能性bot&#xff0c;获得真实的用户反馈&#xff0c;并在周末结束前启动&#xff01; &#xff1f; (Ta…

transition属性值

一、transition-property: transition-property是用来指定当元素其中一个属性改变时执行transition效果&#xff0c;其主要有以下几个值&#xff1a;none(没有属性改变)&#xff1b;all&#xff08;所有属性改变&#xff09;这个也是其默认值&#xff1b;indent&#xff08;元素…

[微信小程序]下拉菜单

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 动画效果是使用CSS3 keyframes 规则(使 div 元素匀速向下移动) <view class page_row><view class"nav" wx:for{{nav_title}} wx:key"index"><vi…

文件解析库doctotext源码分析

doctotext中没有make install选项&#xff0c;make后生成可执行文件在buile目录下面有.so动态库和头文件&#xff0c;需要的可以从这里面拷贝build/doctotext就是可执行程序。doctotext内置了两种检测文件类型方法&#xff1a;1、以后缀为依据检测文件类型2、以内容为依据检测文…

tmux系统剪切板_实践中的tmux:与系统剪贴板集成

tmux系统剪切板by Alexey Samoshkin通过阿列克谢萨莫什金(Alexey Samoshkin) 在实践中使用tmux&#xff1a;与系统剪贴板集成 (tmux in practice: integration with the system clipboard) 如何在tmux复制缓冲区和系统剪贴板之间建立桥梁&#xff0c;以及如何在OSX或Linux系统…

【Java面试题】54 去掉一个Vector集合中重复的元素

在Java中去掉一个 Vector 集合中重复的元素 1)通过Vector.contains&#xff08;&#xff09;方法判断是否包含该元素&#xff0c;如果没有包含就添加到新的集合当中&#xff0c;适用于数据较小的情况下。 import java.util.Vector; public class DeleteVector {public static v…

tornado+nginx上传视频文件

[http://arloz.me/tornado/2014/06/27/uploadvideotornado.html] [NGINX REFRER: Nginx upload module] 由于tornado通过表达上传的数据最大限制在100M&#xff0c;所以如果需要上传视屏文件的情况在需要通过其他方式实现&#xff0c; 此处采用nginx的nginx-upload-module和jQu…

微信小程序swiper组件宽高自适应方法

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 我把 swiper 的 width 设定成了屏幕的95%宽度, 如果想宽度也自适应的话请改成 width:{{width*2}}rpx <swiper classadvertising2 indicator-dots"true" styleheight:{{…

全面访问JavaScript的最佳资源

Looking for a new job is a daunting task. There are so many things to consider when trying to find the perfect role - location, company, job responsibilities, pay and compensation, training and much more.找一份新工作是艰巨的任务。 试图找到理想的职位时&…

Redis集群官方推荐方案 Redis-Cluster

Redis-Cluster redis使用中遇到的瓶颈 我们日常在对于redis的使用中&#xff0c;经常会遇到一些问题 1、高可用问题&#xff0c;如何保证redis的持续高可用性。 2、容量问题&#xff0c;单实例redis内存无法无限扩充&#xff0c;达到32G后就进入了64位世界&#xff0c;性能下降…

[微信小程序]单选框以及多选框实例代码附讲解

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 效果图 <radio-group class"radio-group" bindchange"radioChange"><label class"radio" wx:for"{{k7}}" wx:key"index&q…

IDL_GUI

菜单栏设计 PRO IDLGui;构建界面;显示;添加事件tlbWIDGET_BASE(xsize400,ysize400,/column,mbarmbar);实现基类fileWIDGET_BUTTON(mbar, $ &#xff1b;新建button&#xff0c;value文件)openwidget_button(file,value打开,/menu)jpgwidget_button(open,valuejpg)existwidget_…

git隐藏修改_您可能不知道的有关Git隐藏的有用技巧

git隐藏修改I have launched a newsletter Git Better to help learn new tricks and advanced topics of Git. If you are interested in getting your game better in Git, you should definitely check that out.我已经发布了Git Better通讯&#xff0c;以帮助学习Git的新技…

css 层叠式样式表(2)

一&#xff0c;样式表分类 &#xff08;1&#xff09;内联样式。 --优先级最高&#xff0c;代码重复使用最差。 &#xff08;当特殊的样式需要应用到单独某个元素时&#xff0c;可以使用。 直接在相关的标签中使用样式属性。样式属性可以包含任何 CSS 属性。&#xff09; &…

Hadoop学习笔记之三 数据流向

http://hadoop.apache.org/docs/r1.2.1/api/index.html 最基本的&#xff1a; 1. 文本文件的解析 2. 序列文件的解析 toString会将Byte数组中的内存数据 按照字节间隔以字符的形式显示出来。 文本文件多事利用已有的字符处理类&#xff0c; 序列文件多事创建byte数组&#xff0…

[微信小程序]星级评分和展示(详细注释附效果图)

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 星级评分分成两种情况: 一:展示后台给的评分数据 二:用户点击第几颗星星就显示为几星评分; <!--pages/test/test.wxml--> <view> <view>一:显示后台给的评分</…

uber_这就是我本可以免费骑Uber的方式

uberby AppSecure通过AppSecure 这就是我本可以免费骑Uber的方式 (Here’s how I could’ve ridden for free with Uber) 摘要 (Summary) This post is about a critical bug on Uber which could have been used by hackers to get unlimited free Uber rides anywhere in th…

磁盘I/O 监控 iostat

iostat -cdxm 2 5 dm-4 如果没有这个命令&#xff0c;需要安装sysstat 包。 Usage: iostat [ options ] [ <interval> [ <count> ] ]Options are:[ -c ] [ -d ] [ -N ] [ -n ] [ -h ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ -z ][ <device> [...] | ALL ] [ -p…

[微信小程序]物流信息样式加动画效果(源代码附效果图)

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 效果图片:(信息仅为示例) <!--pages/order/order_wl.wxml--> <view classpage_row top><image classgoods src../../images/dsh.png></image><view cl…