您需要將一些代碼添加到您的pull-from.php文件(或創建另一個使用它來獲取數據的文件),該文件可以接受來自AJAX調用的參數並返回響應,最好是JSON。這基本上是你的網絡服務。
以下是關於php基本需要做什麼的準系統示例。我包含一個從網頁服務提取數據並顯示它的html頁面。
<?php
$criteria = $_POST['criteria'];
function getData($params) {
//Call MySQL queries from here, this example returns static data, rows simulate tabular records.
$row1 = array('foo', 'bar', 'baz');
$row2 = array('foo1', 'bar1', 'baz1');
$row3 = array('foo2', 'bar2', 'baz2');
if ($params) {//simulate criteria actually selecting data
$data = array(
'rows' => array($row1, $row2, $row3)
);
}
else {
$data = array();
}
return $data;
}
$payload = getData($criteria);
header('Content-Type: application/json');//Set header to tell the browser what sort of response is coming back, do not output anything before the header is set.
echo json_encode($payload);//print the response
?>
的HTML
<!DOCTYPE html>
<html>
<head>
<title>Sample PHP Web Service</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<script>
$(document).ready(function(){
alert("Lets get started, data from the web service should appear below when you close this.");
$.ajax({
url: 'web-service.php',
data: {criteria: 42},//values your api needs to query data
method: 'POST',
}).done(function(data){
console.log(data);//display data in done callback
$('#content').html(
JSON.stringify(data.rows)//I just added the raw data
);
});
});
add'$ .get('pull-from.php',function(data){$('#refresh-table')。html(data); }',基本上 –
明白了!非常感謝!我浪費了3個小時。但是學習。 –