为什么服务器端和客户端脚本不能交互? [英] Why can't server side and client side scripts interact?

查看:95
本文介绍了为什么服务器端和客户端脚本不能交互?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是客户端和服务器端脚本的新手,我想知道他们为什么不能互动?

I'm new to client and server side scripts, and I was wondering, how come they can't interact?

主要区别Code Conquest声明这里是:

The major difference Code Conquest stated here is that:


...被称为客户端语言是因为它在您加载网页后在您的计算机上运行脚本。

... is called a client side language is because it runs scripts on your computer after you’ve loaded a web page.


服务器端或后端语言在加载HTML之前运行其脚本,而不是之后。

A server side or back-end language runs its scripts before the HTML is loaded, not after.

即使已经执行了服务器端脚本(例如PHP),它怎么也不能改变(在页面加载后使用JavaScript)?

Even if the server side script (e.g. PHP) has already been executed, how come it can't be changed (with JavaScript) after the page has loaded?

我想从JS调用PHP。例如,有可能这样做吗?

I want to call PHP from JS. For example, is there a possible way to do this?

setInterval(<?php someFunction() ?>,10000);

对不起,如果我误解了什么,请指出给我。

Sorry if I'm misinterpreting something, and please point it out to me.

推荐答案

他们可以互动,但不像你想象的那样。

Well they can interact, but not like you are imagining it.

想想看客户端JavaScript作为浏览器,并考虑服务器端代码,以及服务器。

Think of client side JavaScript as the browser, and think of server side code, well as the server.

然后他们会通过彼此之间发送消息进行通信,这是最常用的方法用于交换消息的是JSON。

Then they would communicate by sending messages between each other, the most commonly used method for exchanging messages would be JSON.

总结一下,客户端代码可以与服务器通信,通过 GET POST 请求,或使用AJAX。

To sum this up, client side code can communicate with the server be sending messages through GET or POST requests, or by using AJAX.

服务器可以响应这些消息,它还可以(这是在HTML 5标准中添加的)使用WebSockets将事件发送到客户端。

The server can respond to those messages, and it can also (this was added in HTML 5 standard) send events to the client using WebSockets.

这篇关于为什么服务器端和客户端脚本不能交互?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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