AJAX调用 - 哪里的逻辑去? [英] AJAX calls - where does logic go?

查看:106
本文介绍了AJAX调用 - 哪里的逻辑去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这甚至可能不会是一个AngularJS问题,可能仅仅是一个AJAX的问题。我是新来的前端的开发商一侧,多多包涵。

This might not even be an AngularJS question and could just be an AJAX question. I'm new to the "developer" side of the frontend so bear with me.

在做一个AJAX调用来获取JSON数据,其中不返回什么数据背后的逻辑和观察的秋天呢?在我的脑海里,将有几种可能性,我想知道这是正确的选择,这是为什么。

When making an AJAX call to fetch JSON data, where does the logic behind what data is returned and viewed fall? In my mind, there would be a couple of possibilities and I want to understand which is the proper choice and why.

让我们使用搜索和播放YouTube视频的例子。

Let's use an example of searching and playing a Youtube video.

  1. 的逻辑可能会下降到后端(控制器),其中,JSON是基于某种逻辑,为您带来完全正确的数据的JSON文件渲染。比如你搜索猫视频,使AJAX调用的时候,你拉JSON文件已经被渲染到只有猫的视频。

  1. The logic could fall to the backend (controller), where the JSON is rendered based on some logic to give you a JSON file with exactly the right data. i.e. you search "cat videos" and when making an AJAX call, the JSON file you pull has been rendered to be only cat videos.

的另一端是,该角控制器具有逻辑。这意味着,所有数据被称为(猫视频以及一切......音乐视频,搞笑视频,教程等等),然后进行排序,通过在客户端。这对我反正会比较低效/客户端慢,所以似乎并没有什么意义。我想还是可以做在客户端的数据进行一些滤波虽然。所以,也许搜索猫视频不会返回所有影片,但肯定所有的猫视频,并根据,说任何过滤的意见,录像长度,#,等会在客户端完成的(相对于再次调用数据库中的一个新设置的视频)。

The opposite end would be that the Angular controller has the logic. This would imply that all data is called (cat videos along with everything else... music videos, funny videos, tutorials, and so on) and then sorted through on the client side. This, to me anyway, would be more inefficient / slow for the client, so doesn't seem to make sense. I suppose still might do some filtering of the data on the client side though. So, maybe a search for "cat videos" wouldn't return ALL videos, but definitely all cat videos and any filtering based on, say, # of views, video length, and so on would be done on the client side (vs. calling the database again for a "new" set of videos).

不知道这是正确的,但你能有逻辑,你的工厂返回数据的一个部分?不过,我相信整个JSON文件需要被渲染,但只有部分将被退回。我猜这取决于所在的JSON文件呈现(即后端或前端),这可能是相似的两种选择1号或2。

Not sure if this is accurate, but could you have logic in your factory to return only a portion of the data? However, I believe the entire JSON file would need to be rendered, but only portions would be returned. I guess depending on where the JSON file renders (i.e. backend or frontend) this could be similar to either option #1 or #2.

也许我误解的事情完全和这个工作的方式是完全不同的!

Or maybe I'm misunderstanding things entirely and the way this works is entirely different!

我基本上希望找出如何1.用户的情况下搜索的术语,结果显示,2。用户点击一个搜索结果,结果现在更详细的数据是在它自己的页面。以及如何结束工作了。我在寻找帮助AngularJS,但我认为这最终一个AJAX的问题(单页的应用程序或没有)比什么都重要。

I'm basically looking to figure out how the scenarios of 1. user searches a term and results are shown, 2. user clicks a search result and now more detailed data of the result is on it's own page. And how this ends up working out. I'm looking for help with AngularJS, but I think this ultimately an AJAX question (single page app or not) more than anything.

推荐答案

有可能混淆了一些重要的概念。

There's a few critical concepts you may be confused about.

第一。 JSON是不是一个文件,它的格式,更简单地说,一个字符串类型。这对倒塌阵列和存储地址 - 值对真正的好,所以很多数据的苍蝇围绕该格式。严格意义上来说,他们是JSON对象,但他们很像字符串和数组。它看起来像这样,如果我没记错的话:

First. JSON is not a file, it's a format, more simply, a type of string. It's really good for collapsing arrays and storing address-value pairs, so a lot of data flies around in that format. Strictly speaking, they are JSON objects, but they're a lot like strings and arrays. It looks like this, if I remember correctly:

{ "name" : "john doe", "pet" : "dog", "hobby" : "parasailing" }


其次,AJAX是一个请求给服务器,从客户端(浏览器)的原始页面加载之后进行。也就是说,你在youtube.com类型,并在YouTube服务器接收请求和发送一大堆的HTML返回到浏览器。


Second, AJAX is a request to the server, made from the client (the browser) after the original page has loaded. That is, you type in 'youtube.com' and the youtube server receives the request and sends a big pile of HTML back to your browser.

您观看您的视频,进行评级,并在浏览器的不重新加载页面的,而是发送的单独的的请求到YouTube的服务器,你的等级。有没有在说把它给ratingspage.php请求中的参数。这个请求是AJAX。

You watch your video, make a rating, and the browser doesn't reload the page but instead sends a separate request back to the youtube server with your rating. There's a parameter in the request that says "send it to ratingspage.php". This request is AJAX.

现在,逻辑操作(服务器端)。 ratingspage.php 收到您的申请。其接触的数据库,更新或失败或什么,并发送回浏览器的响应。这种反应可能是JSON格式。

Now, the logic happens (server-side). ratingspage.php receives your request. It contacts the databases, updates or fails or whatever, and sends back a response to your browser. This response may be in JSON format.

最后,您的浏览器解析该响应并更新DOM(HTML文档)适当。

Finally, your browser parses that response and updates the DOM (HTML document) as appropriate.

在这一点上,值得注意的是,如果该逻辑发生在客户端(浏览器),用户可以看到它 - 这是一个安全问题!因此,敏感的操作应进行在服务器端,在那里你可以测试和消毒的请求数据。

At this point, it's worth noting that if the logic happened on the client-side (browser), the user could see it - this is a security problem! So, sensitive operations should be carried out on the server side, where you can test and sanitize the request data.

在总结:

  • 在AJAX是独立于初始加载事件。
  • 发送的信息从客户端浏览器聚集
  • 在逻辑发生服务器端
  • 逻辑可以用任何一种语言的服务器理解(PHP,Java和Ruby的,等等。)
  • 的信息被返回到浏览器
  • 在发送和接收可以使用JSON格式的信息
  • 一切客户端发生的Javascript

下面是一个最基本的Ajax请求(在Javascript中完成)有意见。这有没有异常处理,状态检查,或任何所以不要使用它!但是,它给你的基本思路。

Here's a bare-bones ajax request (done in Javascript) with comments. This has no exception handling, state checking, or anything so don't use it! But it gives you the basic idea.

// Make a new request
var req = new XMLHttpRequest(); }                               

// Requests will have various states depending on whether they're processing,
// finished, error, etc.  We'll assume everything went OK.
// We need to establish a handler before the request
// is sent so it knows what to do.
req.onreadystatechange = function() {
    // Here's what the server sent back to the browser
    alert(req.responseText);
}

// Using the GET method, set up some parameters
req.open("GET", "somelogicpage.php?blah=blee&bloo=bar", true);
// Send the request
req.send(null);

服务器端, somelogicpage.php 可能看起来像:

<?php
if ($_GET['blah'] != 'blee']) {
    // This is the response text!
    echo "Sorry, you need to blee when you blah.";
}
else {
    // (or this)
    echo "I'm ecstatic to report nothing is wrong!";
}
?>

警报(req.responseText)从previous处理函数的Javascript会说什么的PHP已经倾倒了。

Your alert(req.responseText) from the handler function in the previous Javascript will say whatever the PHP has dumped out.

所以,是的,你可以用你喜欢的请求的任何部分,然后返回任何你喜欢的。 Javascript的踢嘟嘟。

So yes, you can use whatever portion of the request you like, and return whatever you like. Javascript kicks bleep.

这篇关于AJAX调用 - 哪里的逻辑去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆