简体   繁体   English

Web服务中的JSON

[英]JSON in web services

I know that JSON can be used between 2 web servers, for example facebook API provides some infos using aFile.json so my website need to handle it using PHP's json_decode() function, 我知道JSON可在2个网络服务器之间使用,例如facebook API使用aFile.json提供了一些信息,因此我的网站需要使用PHP的json_decode()函数来处理它,

the problem that I can't imagine any pratical situation wherein we can use it between a server and his client to exchange data. 这个问题使我无法想象任何实际情况,我们可以在服务器和他的客户端之间使用它来交换数据。 If the client request for some data, the server will use either SQL to retrieve them from a database or a JSON recieved file or whatever else... but finally he will respond only by an HTML file (& probabely some images) so the client's browser can interpret it, 如果客户端请求某些数据,则服务器将使用SQL从数据库或JSON接收文件或其他任何内容中检索它们……但最终,他将通过HTML文件(可能包含一些图像)进行响应,因此客户端的浏览器可以解释它,

Am I wrong and the JSON files can be sent to the client ? 我错了,可以将JSON文件发送到客户端吗? then why ? 那么为什么 ? I readed also that JSON is used to communicate between PHP and javascript in the same server , can you give me an example of this case ? 我还读到JSON用于在同一服务器上的 PHP和javascript之间进行通信,您能举个例子吗?

You can think about it in this way: 您可以这样考虑:

  1. You go to the website X, and your browser (client) sends request to the server, for particular page. 您转到网站X,浏览器(客户端)将请求发送到特定页面的服务器。 In this case, server will respons with HTML page wich will be rendered by your browser to you. 在这种情况下,服务器将以HTML页面响应,而该页面将由您的浏览器呈现给您。
  2. You already are on website X, your browser already rendered a page for you, and now you have input with newsletter subscription, you put your e-mail in this input and click 'subscribe'. 您已经在网站X上,您的浏览器已经为您创建了一个页面,现在您已经输入了订阅新闻通讯的内容,将您的电子邮件放入此输入中,然后单击“订阅”。 Here one of the scenarios is that JavaScript will post your e-mail to the server using JSON-format, and server will respond to the JavaScript associated with your HTML some confirmation (also in JSON-format), which will be displayed for you. 在这种情况下,其中一种情况是JavaScript将使用JSON格式将您的电子邮件发布到服务器,并且服务器将对与HTML关联的JavaScript做出一些确认(也是JSON格式)的响应,这将为您显示。

This second scenario might be used in order to avoid reloading the page which is already render for You. 为了避免重新加载已经为您呈现的页面,可以使用第二种情况。

You can go to developer tools in your browser, you can display section network , and You can observe different HTTP messages which are sent back and forth between client and server. 您可以在浏览器中转到开发人员工具 ,可以显示network区域 ,并且可以观察到客户端和服务器之间来回发送的不同HTTP消息。

For more information I suggest reading https://developer.mozilla.org/pl/docs/Web/HTTP because they have nice documentation regarding HTTP there. 有关更多信息,我建议阅读https://developer.mozilla.org/pl/docs/Web/HTTP,因为他们在那里有关于HTTP的不错的文档。

JSON is basically a JavaScript Object (JSON stands for JavaScript Object Notation) in text. JSON基本上是文本中的JavaScript对象(JSON表示JavaScript对象表示法)。

You can fetch JSON data from the server with an AJAX call. 您可以使用AJAX调用从服务器获取JSON数据。 This is a simplified JavaScript example that makes an AJAX call and then assigns the response JSON to var myObj . 这是一个简化的JavaScript示例,该示例进行AJAX调用,然后将响应JSON分配给var myObj

var myObj, xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
   myObj = JSON.parse(xmlhttp.responseText);
  }
};
xmlhttp.open("GET", "/example", true);
xmlhttp.send();

Servers response 服务器响应

{"name":"John", "surname":"Doe"}

Printing the received data to HTML element "demo" after the response has been assigned to myObj 将响应分配给myObj之后,将接收到的数据打印到HTML元素“ demo”

document.getElementById("demo").innerHTML = myObj.name + " " + myObj.surname;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM