简体   繁体   English

点击javascript按钮添加好友

[英]Click javascript button to add friend

On Facebook you can click on the "add friend" button to add a friend without refreshing the page. 在Facebook上,您可以单击“添加朋友”按钮添加朋友而不刷新页面。 Presumably this involves accessing a database using JavaScript, but I was given to understand that that is a bad idea. 据推测,这涉及使用JavaScript访问数据库,但我理解这是一个坏主意。

Could anyone explain this to me? 谁有人向我解释这个?

This is not directly interacting with the database but with an API ( some web service ). 这不是直接与数据库交互,而是使用API​​(某些Web服务)。 The API then has logic within it to then interact with the database. 然后,API中包含逻辑,然后与数据库进行交互。

Its Ajax basicly 它的Ajax基本上

  1. User click button 用户点击按钮
  2. Javascript open another page at background like "addfriend.php?id=5" Javascript在后台打开另一个页面,例如“addfriend.php?id = 5”
  3. PHP page add request to Database PHP页面向数据库添加请求
  4. PHP (or any server page) return answer PHP(或任何服务器页面)返回答案
  5. Javascript handle answer Javascript处理答案

They do not allow database access from javascript, they are simple making HTTP calls from javascript, without loading entire page. 他们不允许从javascript访问数据库,他们很容易从javascript进行HTTP调用,而无需加载整个页面。 The technology is called AJAX (Asynchronous JavaScript and XML). 该技术称为AJAX(异步JavaScript和XML)。 You can read more about AJAX on Wikipedia . 您可以在Wikipedia上阅读有关AJAX的更多信息。

It would have been bad if the SQL for the database would be specified in the client code and executed without any validation against the DB: 如果数据库的SQL将在客户端代码中指定并且在没有对DB进行任何验证的情况下执行,那将会很糟糕:

<input type="button" onclick="updateDatabase('INSERT INTO FriendRequests...')" />

...but that is not the case. ...但事实并非如此。

Very simplified: The 'add friend' button calls a server-side method via an HTTP request. 非常简化:“添加好友”按钮通过HTTP请求调用服务器端方法。 The server-side method verifies your identity and the request, then probably calls a data-access method, and the data-access method then calls the underlying database. 服务器端方法验证您的身份和请求,然后可能调用数据访问方法,然后数据访问方法调用底层数据库。 So this is perfectly acceptable. 所以这是完全可以接受的。

JavaScript导致浏览器在幕后发出HTTP请求 (通常称为Ajax),服务器端进程处理数据库访问。

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

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