且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AJAX调用 - 哪里的逻辑去?

更新时间:2023-11-29 22:10:46

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

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是一个请求给服务器,从客户端(浏览器)的原始页面加载之后进行。也就是说,你在***.com类型,并在***服务器接收请求和发送一大堆的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 '***.com' and the *** server receives the request and sends a big pile of HTML back to your browser.

您观看您的视频,进行评级,并在浏览器的不重新加载页面的,而是发送的单独的的请求到***的服务器,你的等级。有没有在说把它给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 *** 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.