我们可以使用AJAX调用数据库吗?

Kes*_*Van 0 ajax jquery jquery-mobile

我创建了一个html文件,现在创建下一个文件,只使用AJAX代码从localhost调用数据库,有没有办法调用数据库并使用AJAX代码从数据库中检索数据.这是我在下面给出的代码..

data.html

<!DOCTYPE HTML>
<html>
  <head>
    <title>My DATABASE</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
  </head>
  <body>
    <h1>EMPLOYEE DATABASE</h1>
    <center><div data-role="page" id="pageone">
      <div data-role="header">
        <h1>Welcome To My DATABASE</h1>
      </div>
      <div data-role="content">
        <p>Welcome!</p><br><br>
        <img src="logo.png"></img></p><br>
        <a href="#pagetwo" data-role="button" data-inline="true">View Employee</a><br>
        <a href="#pagetwo" data-role="button" data-inline="true">Add Employee</a>
      </div>
    </center>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

empl.html

<!DOCTYPE HTML>
<html>
  <head>
    <title>LIST OF EMPLOYEE</title>
    <script type="text/javascript" charset="utf-8" src="cordova-1.9.0.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
    <script> 
      $.ajax({
        $host = "localhost";
        $user = "root";
        $pass = "123";
        $databaseName = "student1";
        $tableName = "employee";
        $con = mysql_connect($localhost,$root,$123);
        $dbs = mysql_select_db($student, $con);
        $result = mysql_query("SELECT * FROM $employee");        
        $array = mysql_fetch_row($result);                            
        echo json_encode($array);
       }
      });      
    </script>
  </head>
  <body>
    <div data-role="header">
      <h1> EMPLOYEES</h1>
    </div>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Ron*_*Dex 5

这取决于您使用的数据库类型.他们可能在数据库服务器上运行一些Web服务,使您可以访问数据库.但是,使用AJAX直接从javascript访问数据库是一个非常糟糕的做法,并打开安全漏洞.我强烈建议您创建自己的Web服务并通过它访问数据.