AJAX はデータベース データを読み取ります
データベース データを読み取るための Ajax コード例:
Ajax はバックグラウンドにリクエストを送信し、パラメーターを渡すことができるため、もちろん、必要に応じてデータベース クエリ関数を実行できます。
以下は、この要件を達成する方法を紹介するコード例です。もちろん、このコードは比較的単純であり、参照用です。
コード例:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="author" content="//m.sbmmt.com/" />
<title>php中文网</title>
<style>
body{
font-size:12px;
}
</style>
<script>
function loadXMLDoc(keywords) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("show").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("get", "demo/ajax/net/Query.aspx?keywords=" + escape(keywords), true);
xmlhttp.send();
}
window.onload = function () {
var otxt = document.getElementById("txt");
var obt = document.getElementById("bt");
obt.onclick = function () {
loadXMLDoc(otxt.value);
}
}
</script>
</head>
<body>
<input type="text" id="txt"/>
<input type="button" id="bt" value="查看效果"/>
(例如输入:css教程或者div教程)
<div>结果:<span id="show"></span></div>
</body>
</html>テキストボックスにクエリしたいコース名を入力し、ボタンをクリックしてクエリ効果を実現します。 asp.net の背景コードは次のとおりです:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Configuration;
namespace ajax
{
public partial class Query : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string keywords = Server.UrlDecode(Request.QueryString["keywords"]);
if (String.IsNullOrEmpty(keywords.Trim()))
{
Response.Write("输入查询关键词");
Response.End();
}
string connString=ConfigurationManager.ConnectionStrings["access_con"].ConnectionString;
string configPath=ConfigurationManager.ConnectionStrings["access_path"].ConnectionString;
string conPath = HttpContext.Current.Server.MapPath(configPath);
OleDbConnection conn = new OleDbConnection(connString + conPath);
string sql = "select * from data where type='" + keywords + "' order by id desc";
OleDbCommand cmd = new OleDbCommand(sql,conn);
try
{
conn.Open();
OleDbDataReader MyReader = cmd.ExecuteReader();
if (MyReader.Read())
{
Response.Write("存在指定教程");
}
else
{
Response.Write("不存在指定教程");
}
}
catch (Exception ex)
{
throw (ex);
}
finally
{
conn.Close();
}
}
}
}

