Home >Web Front-end >JS Tutorial >How to pass variables defined in servlet to javascript function

How to pass variables defined in servlet to javascript function

醉折花枝作酒筹
醉折花枝作酒筹Original
2021-07-22 15:27:022156browse

JS needs to obtain data from the servlet. When data is needed, js accesses the server's servlet through some mechanism (such as ajax). The servlet is executed on the server based on the parameters passed when js accesses the server (no parameters are allowed). Calculate, then return the result, and then js can process the returned data.

How to pass variables defined in servlet to javascript function

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

First of all, we must clarify the difference between the server and the client

The browser belongs to the client, and what is processed by the user's computer on the browser side

The server side belongs On the server side, the data on the network is basically obtained from the server side

Then, the servlet processes the data on the server side, while js belongs to the client

js needs to obtain data from the servlet, When data is needed, js accesses the server's servlet through some mechanism (such as ajax).

The servlet performs calculations on the server based on the parameters passed when js accesses the server (no parameters are required), and then returns the result (generally Return json data), and then js can process the returned data

For example, the ajax method in the JS framework jquery

$.ajax(
'servlet的服务器地址’,
'{'数据的键key':'向服务器提交的数据的值value’,‘其他的键key2':'其他数据值value2’}',
success:function(servlet返回的数据变量){
数据交互成功后的处理函数,在这里对你的页面动态更改吧
}
);

Of course, the process of interaction between the client and the server, in addition to the above success status, there will be three other different statuses. According to different statuses, you can also process them separately

Lastly, newer JS frameworks such as node.js greatly simplify the data interaction between the client and the server. process, for applications with heavy data exchange between client and server (such as Weibo, etc.), using it can greatly improve performance

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to pass variables defined in servlet to javascript function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn