我尝试在我的网络项目中更多地使用jQuery / Ajax,作为我做过的测试: HTML代码
<div id="main_content"></div>
<div class="panel panel-primary">
<div class="panel-heading">API Credits Left.</div>
<div class="panel-body">
<form id="frmAjax" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" class="form-horizontal container-fluid" role="form">
<div class="row form-group">
<div class="col-sm-12 text-right">
<button type="submit" name="submit" class="btn btn-default">Check the API credits...</button>
</div>
</div>
</form>
</div>
<div class="panel-footer"><b>DomDetailer</b> & <b>SeoKicks</b> Api credits left.</div>
</div>
脚本
$(function() {
$(document).ready(function(){
$('#frmAjax').submit(function() {
$("#main_content").load('ajax-credits.php');
});
});
});
ajax-credits.php 内容:
<?php
echo "Loading...";
// http://site1.com/api/checkBalance.php?apikey=QRUE8VTQMD5Fj6&app=
$json1 = file_get_contents('http://site1.net/api/checkBalance.php?apikey=QRUE8VTQMD5F6&app=');
$data1 = json_decode($json1, true);
$valu1 = $data1[1];
// seokicks
$json2 = file_get_contents('http://www.site2.net/V1/inlinkData?appid=p2hFacSWU0&output=json&details=api_credits');
$data2 = json_decode($json2);
$valu2 = $data2->Overview->credits->available;
// display
stdmsg("Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.");
?>
非常基本的代码(或者我认为)任何人都可以看到我做错了什么? div中没有显示任何内容,有什么问题吗?
答案 0 :(得分:2)
尝试删除echo "Loading";
并替换
stdmsg("Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.");
通过
echo "<p>Site 1 has <b>".$valu1."</b> credits left and Site 2 has <b>".$valu2."</b> credits left.</p>";
(无论你在jQuery AJAX函数调用的PHP脚本中回应什么,都会被发送回AJAX函数,因此这将被加载到你的HTML div中。)