Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
了清楚地說明使用AJAX從數(shù)據(jù)庫訪問信息的難易程度,我們將動態(tài)構(gòu)建MySQL查詢并將結(jié)果顯示在“ ajax.html”上。但是在繼續(xù)之前,讓我們做基礎(chǔ)工作。使用以下命令創(chuàng)建表。
注 –我們假設(shè)您具有足夠的特權(quán)來執(zhí)行以下MySQL操作。
CREATE TABLE 'ajax_example' (
'name' varchar(50) NOT NULL,
'age' int(11) NOT NULL,
'sex' varchar(1) NOT NULL,
'wpm' int(11) NOT NULL,
PRIMARY KEY ('name')
)
現(xiàn)在,使用以下SQL語句將以下數(shù)據(jù)轉(zhuǎn)儲到該表中:
INSERT INTO 'ajax_example' VALUES ('Jerry', 120, 'm', 20);
INSERT INTO 'ajax_example' VALUES ('Regis', 75, 'm', 44);
INSERT INTO 'ajax_example' VALUES ('Frank', 45, 'm', 87);
INSERT INTO 'ajax_example' VALUES ('Jill', 22, 'f', 72);
INSERT INTO 'ajax_example' VALUES ('Tracy', 27, 'f', 0);
INSERT INTO 'ajax_example' VALUES ('Julie', 35, 'f', 90);
現(xiàn)在讓我們擁有客戶端HTML文件ajax.html,它將具有以下代碼-
<html>
<body>
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxFunction() {
var ajaxRequest; // The variable that makes Ajax possible!
try {
// Opera 8.0+, Firefox, Safari
ajaxRequest=new XMLHttpRequest();
} catch (e) {
// Internet Explorer Browsers
try {
ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange=function() {
if(ajaxRequest.readyState==4) {
var ajaxDisplay=document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML=ajaxRequest.responseText;
}
}
// Now get the value from user and pass it to
// server script.
var age=document.getElementById('age').value;
var wpm=document.getElementById('wpm').value;
var sex=document.getElementById('sex').value;
var queryString="?age=" + age ;
queryString +="&wpm=" + wpm + "&sex=" + sex;
ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
ajaxRequest.send(null);
}
//-->
</script>
<form name='myForm'>
Max Age: <input type='text' id='age' /> <br />
Max WPM: <input type='text' id='wpm' /> <br />
Sex:
<select id='sex'>
<option value="m">m</option>
<option value="f">f</option>
</select>
<input type='button' onclick='ajaxFunction()' value='Query MySQL'/>
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
注 –在查詢中傳遞變量的方式符合HTTP標(biāo)準(zhǔn),并具有formA。
URL?variable1=value1;&variable2=value2;
上面的代碼將為您提供如下屏幕-
最高年齡:
最高WPM:
性別: 米 F
輸入后,結(jié)果將顯示在此部分中。
注意 -這是一個虛擬屏幕。
您的客戶端腳本已準(zhǔn)備就緒。現(xiàn)在,我們必須編寫服務(wù)器端腳本,該腳本將從數(shù)據(jù)庫中獲取年齡,wpm和性別,并將其發(fā)送回客戶端。將以下代碼放入文件“ ajax-example.php”中。
<?php
$dbhost="localhost";
$dbuser="dbusername";
$dbpass="dbpassword";
$dbname="dbname";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$age=$_GET['age'];
$sex=$_GET['sex'];
$wpm=$_GET['wpm'];
// Escape User Input to help prevent SQL Injection
$age=mysql_real_escape_string($age);
$sex=mysql_real_escape_string($sex);
$wpm=mysql_real_escape_string($wpm);
//build query
$query="SELECT * FROM ajax_example WHERE sex='$sex'";
if(is_numeric($age))
$query .=" AND age <=$age";
if(is_numeric($wpm))
$query .=" AND wpm <=$wpm";
//Execute query
$qry_result=mysql_query($query) or die(mysql_error());
//Build Result String
$display_string="<table>";
$display_string .="<tr>";
$display_string .="<th>Name</th>";
$display_string .="<th>Age</th>";
$display_string .="<th>Sex</th>";
$display_string .="<th>WPM</th>";
$display_string .="</tr>";
// Insert a new row in the table for each person returned
while($row=mysql_fetch_array($qry_result)) {
$display_string .="<tr>";
$display_string .="<td>$row[name]</td>";
$display_string .="<td>$row[age]</td>";
$display_string .="<td>$row[sex]</td>";
$display_string .="<td>$row[wpm]</td>";
$display_string .="</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .="</table>";
echo $display_string;
?>
現(xiàn)在嘗試在“ 最大年齡”或任何其他框中輸入有效值(例如120),然后單擊“查詢MySQL”按鈕。
最高年齡:
最高WPM:
性別: 米 F
輸入后,結(jié)果將顯示在此部分中。
如果您已成功完成本課程,那么您將知道如何結(jié)合使用MySQL,PHP,HTML和Javascript編寫AJAX應(yīng)用程序。
JAX上傳的用戶體驗更好,HTML上傳用戶使用更方便一點,直接在網(wǎng)頁里面就能夠操作了。示例在下面提供了,是完整的源代碼,有教程,有視頻教程,基本上使用非常簡單,開發(fā)也非常簡單,有技術(shù)支持,
網(wǎng)上搜了一下,基本上講這塊的文章還是很多,但是就是一個字亂,講的很混亂。也沒有提供完整的前后端示例。
用戶上傳的文件比較大,有20G左右,直接用HTML傳的話容易失敗,服務(wù)器也容易出錯,需要分片,分塊,分割上傳。也就是將一個大的文件分成若干個小文件塊來上傳,另外就是需要實現(xiàn)秒傳功能和防重復(fù)功能,秒傳就是用戶如果上傳過這個文件,那么直接在數(shù)據(jù)庫中查找記錄就行了,不用再上傳一次,節(jié)省時間,實現(xiàn)的思路是對文件做MD5計算,將MD5值保存到數(shù)據(jù)庫,算法可以用MD5,或者CRC,或者SHA1,這個隨便哪個算法都行。
分片還需要支持?jǐn)帱c續(xù)傳,現(xiàn)在HTML5雖然提供了信息記錄功能,但是只支持到了會話級,也就是用戶不能關(guān)閉瀏覽器,也不能清空緩存。但是有的政府單位上傳大文件,傳了一半下班了,明天繼續(xù)傳,電腦一關(guān)結(jié)果進度信息就丟失了,這個是他們的一個痛點。
切片的話還有一點就是在服務(wù)器上合并,一個文件的所有分片數(shù)據(jù)上傳完后需要在服務(wù)器端進行合并操作。
1.下載示例
https://gitee.com/xproer/up6-vue-cli
將up6組件復(fù)制到項目中
示例中已經(jīng)包含此目錄
1.引入up6組件
2.配置接口地址
接口地址分別對應(yīng):文件初始化,文件數(shù)據(jù)上傳,文件進度,文件上傳完畢,文件刪除,文件夾初始化,文件夾刪除,文件列表
參考:http://www.ncmem.com/doc/view.aspx?id=e1f49f3e1d4742e19135e00bd41fa3de
3.處理事件
啟動測試
啟動成功
效果
數(shù)據(jù)庫
ajax 是前后端交互的重要手段或橋梁。它不是一個技術(shù),是一組技術(shù)的組合。
ajax :a:異步;j:js;a:和;x:服務(wù)端的數(shù)據(jù)。
ajax的組成:
通過后臺與服務(wù)器進行少量數(shù)據(jù)交換,ajax可以使網(wǎng)頁實現(xiàn)異步更新。也就是在不需要重新加載整個網(wǎng)頁的情況下,能夠更新部分網(wǎng)頁的技術(shù)。傳統(tǒng)的網(wǎng)頁不使用ajax,如果需要更新內(nèi)容,必須重新加載整個頁面。
ajax請求原理:創(chuàng)建一個網(wǎng)絡(luò)請求對象 -> 發(fā)送連接請求 -> 發(fā)送請求數(shù)據(jù) -> 檢查網(wǎng)絡(luò)請求對象的狀態(tài) -> 如果響應(yīng)成功了 -> 瀏覽器接收返回數(shù)據(jù)并更新網(wǎng)頁。接下來詳細(xì)介紹對象的創(chuàng)建以及它的方法。
XMLHttpRequest 對象,用于后臺與服務(wù)器之間的數(shù)據(jù)交換,意味著可以在不加載整個網(wǎng)頁的情況下,更新部分內(nèi)容或數(shù)據(jù)?,F(xiàn)代瀏覽器基本都支持,但是低版本的IE不支持,如果我們考慮IE兼容問題創(chuàng)建對象的時候需要兼容創(chuàng)建。
考慮兼容時創(chuàng)建的對象:
var xhr ;
if( window.XMLHttpRequest ){ //檢查瀏覽器是否支持XMLHttpRequest
xhr=new XMLHttpRequest()
}else{
xhr=new ActiveXObject("Microsoft.XMLHTTP") //兼容IE6 IE5
}
3.1、open( )
設(shè)置請求的類型、請求接口、是否異步處理。
使用語法:open( method , url , async )
3.2、send( )
將請求發(fā)送到服務(wù)器。
使用語法:send( string )
使用發(fā)送方式不同的時候,傳輸數(shù)據(jù)添加方式也不同,所以我們介紹下分別為post和get時,數(shù)據(jù)是如何發(fā)送的?
3.3、提交方式
get發(fā)送請求時,需要傳給后臺的數(shù)據(jù)通過url來傳遞,多個參數(shù)之間使用 & 符號連接,使用時如下:
xhr.opn( "GET" , "1.php?name=hello&age=world" , true )
xhr.send()
使用 post 方式發(fā)送請求時,使用send來發(fā)送數(shù)據(jù),有時需要設(shè)置數(shù)據(jù)格式,類似表單那樣,此時可通過 setRequestHeader 設(shè)置發(fā)送的數(shù)據(jù)格式
xhr.setRequestHeader( "Content-type", "application/x-www-form-urlencoded")
Content-type常見類型:
readyState 存有 XMLHttpRequest 的狀態(tài),它的值從 0-4 發(fā)生變化,分別代表的意義:
每當(dāng) readyState 狀態(tài)值發(fā)生改變時會,就會觸發(fā) onreadystatechange 事件,對應(yīng)著每個狀態(tài)值就會被觸發(fā)五次。當(dāng)狀態(tài)值為 4 時表示網(wǎng)絡(luò)請求響應(yīng)完畢,就可以獲取返回的值。
xhr.onreadystateChange=function(){
if( xhr.readyState==4 ){
if( xhr.status>=200 && xhr.status<300 || xhr.status==304 ){
console.log("請求成功",xhr.responseXML)
}else{
console.log("請求失敗")
}
}
}
通常我們需要獲取服務(wù)器返回的信息,然后對我們的網(wǎng)頁做相對應(yīng)的結(jié)果展示,通常使用 XMLHttpRequest 的 responseText 或 responseXML 屬性。
responseText ---> 獲取到的是字符串形式。接收到可直接使用,無需轉(zhuǎn)換。
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
responseXML ---> 獲取到 XML 形式的數(shù)據(jù)。使用時需要解析,如:
<person>
<name>小米粒</name>
<age>18</age>
</person>
解析時:
document.getElementsByTagName("name")[0]
responseXML 目前已被 json 取代,所以作為了解就好。
var xhr ;
if( window.XMLHttpRequest ){
xhr=new XMLHttpRequest()
}else{
xhr=new ActiveXObject("Microsoft.XMLHTTP") //兼容IE6 IE5
}
xhr.open('GET','1.txt',true)
xhr.send()
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status>=200 && xhr.status<300 || xhr.status==304){
console.log("請求成功",xhr.response) // 請求成功 abc
}else{
console.log("請求失敗")
}
}
}
1.txt 文檔內(nèi)容為 abc。所以返回的結(jié)果也是abc
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。