简体   繁体   English

用Java搜索网站功能的正确方法是什么?

[英]What is the right way for Searching functions in a Website with Javascript?

Its known that interactions between Javascript and SQL-Databases are not very secure. 众所周知,JavaScript与SQL数据库之间的交互不是很安全。 But most Websites use it cause the Webside doesent reload to show matches in a search. 但是大多数网站使用它会导致Webside重新加载以在搜索中显示匹配项。

With PHP it isn't possible to change Page-Contents without a completely Page-Refreshing. 使用PHP,如果没有完全的页面刷新就无法更改页面内容。

Witch is the right way to get Data from SQL with Javascript without security-neglects. Witch是使用Java脚本从SQL中获取数据而又不忽略安全性的正确方法。 Aspeccialy for a Searching function with directly matches in a list. 专门用于列表中直接匹配的“搜索”功能。

You can use 2 way to get data from db by using js; 您可以使用2种方式通过js从db获取数据;

1. Ajax: 1. Ajax:

function refresh() {
    $.ajax({
        url:"your url",
        method: "GET",
        data: your_params,
        success: function(response) {
            $("#specific_div_id").html(response);
        }
    });
}

You can do this within an interval like; 您可以在类似的时间间隔内执行此操作;

setInterval(refresh, 5000); 

in order to get content in every 5 sec. 为了每5秒获得内容。

2. Websockets 2. Websockets

In AJAX, you are requesting in every 5 secs to get updated content from server. 在AJAX中,您每5秒请求从服务器获取更新的内容。 Think that, you are not getting content server pushes updated content to you. 认为,您没有让内容服务器将更新的内容推送给您。 In other words, server notifies you on any updated data. 换句话说,服务器会通知您任何更新的数据。 You can have a look at Socket.io for an example implementation of websockets . 您可以在Socket.io中查看websockets的示例实现。 When server notifies you, you can take data and put it related html area 当服务器通知您时,您可以获取数据并将其放入相关的html区域

As mention in the commentaries, the best way is to use AJAX, which is an acronym that stands for Asynchronous Javascript and XML. 正如评论中提到的,最好的方法是使用AJAX,它是异步Javascript和XML的缩写。 The last part, XML, is a bit misleading. 最后一部分,XML,有点误导。 It kept that name because that's what is was first use for. 它之所以保留该名称,是因为这是最初的用途。 But AJAX can now be use to make HTTP request and interface with any language, including PHP. 但是现在可以使用AJAX发出HTTP请求并与任何语言(包括PHP)进行接口。

Depending on the technology you are built on, there are several implementation available. 根据您所使用的技术,有几种可用的实现。 Chances are you have jQuery installed already. 您可能已经安装了jQuery。 In that case, jQuery Ajax , and particularly jQuery.get() would address your concerns. 在这种情况下, jQuery Ajax ,尤其是jQuery.get()将解决您的问题。

If you are using a router on the backend, you can simply call a route, specifying it as the url, first argument of the function. 如果您在后端使用路由器,则可以简单地调用路由,将其指定为该函数的第一个参数url。 Otherwise, you can directly call a file by using the relative path from the html page the javascript is embedded in. jQuery.get will return anything you echo within you server script. 否则,你可以直接使用从HTML页面的JavaScript嵌入在相对路径调用文件。jQuery.get将返回任何你echo你的服务器脚本中。 In other words, anything that is directly rendered on the page. 换句话说,任何直接呈现在页面上的东西。 You can use a callback catch the data returned and process it. 您可以使用回调捕获返回的数据并进行处理。 Example : 范例:

$.get('/path/to/file.php', function (data) {
    console.log('Here is the data received from the server!', data)
    // Process data here
});

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

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