我正在尝试对我的PHP脚本进行AJAX调用。我可以回应我的数据结果。 PHP就好了。我的问题是如何从index.html
拨打电话以将数据拉入data.php。
<?php
$pullData = file_get_contents('https://api.rentcafe.com/rentcafeapi.aspx?requestType=apartmentavailability&APIToken=OTI4MjI%3D-eiBNyIvyQA8%3D&propertyCode=p0494361');
$results = json_decode($pullData);
//Table columns
echo '<table>
<tr>
<th>Apartment Name </th>
<th>Beds</th>
<th>Baths</th>
<th>Floor Plan Name</th>
<th>Minumum Rent</th>
<th>Maximum Rent</th>
</tr>';
//Iterate throught the API data and return only required columns
foreach($results as $formatted_results){
echo '<tr>';
echo '<td>'.$formatted_results->ApartmentName.'</td>';
echo '<td>'.$formatted_results->Beds.'</td>';
echo '<td>'.$formatted_results->Baths.'</td>';
echo '<td>'.$formatted_results->FloorplanName.'</td>';
echo '<td>'.$formatted_results->MinimumRent.'</td>';
echo '<td>'.$formatted_results->MaximumRent.'</td>';
echo '</tr>';
}
echo '</table>';
?>
答案 0 :(得分:0)
一个简单的get会做到。
$.get("data.php", function(data){ // GET data from page "data.php"
$("#result").html(data); // display data in a div with id "result"
});
答案 1 :(得分:0)
考虑您的index.html是否包含以下代码,然后您可以在html文件的脚本中调用ajax调用。这是工作代码(考虑index.html和date.php都在同一个地方)
的index.html
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<div class="result">
</div>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
$.get( "data.php", function( data ) {
$( ".result" ).html( data );
});
});
</script>
data.php文件包含您给出的代码
data.php
<?php
$pullData = file_get_contents('https://api.rentcafe.com/rentcafeapi.aspx?requestType=apartmentavailability&APIToken=OTI4MjI%3D-eiBNyIvyQA8%3D&propertyCode=p0494361');
$results = json_decode($pullData);
//Table columns
echo '<table>
<tr>
<th>Apartment Name </th>
<th>Beds</th>
<th>Baths</th>
<th>Floor Plan Name</th>
<th>Minumum Rent</th>
<th>Maximum Rent</th>
</tr>';
//Iterate throught the API data and return only required columns
foreach($results as $formatted_results){
echo '<tr>';
echo '<td>'.$formatted_results->ApartmentName.'</td>';
echo '<td>'.$formatted_results->Beds.'</td>';
echo '<td>'.$formatted_results->Baths.'</td>';
echo '<td>'.$formatted_results->FloorplanName.'</td>';
echo '<td>'.$formatted_results->MinimumRent.'</td>';
echo '<td>'.$formatted_results->MaximumRent.'</td>';
echo '</tr>';
}
echo '</table>';
?>