我对这个php比较新,我试图从我的javascript文件访问/执行一个php文件,并让它出现在我的html中。有人告诉我,$的getJSON是做到这一点的最好的方式,但我看不到我的PHP脚本过去被逼到我的html:我的jquery没有访问php文件
HTML:
<!doctype html>
<html lang="en">
<head>
<div class="button"></div>
<script src="http://code.jquery.com/jquery-1.7.js"></script>
<script src="Test.js" type="text/javascript"></script>
</head>
<body>
<a href="#" id="getdata-button">Get JSON Data</a>
<div id="showdata"></div>
</body>
</html>
的Javascript:
$(document).ready(function(){
$(document).on("click","#getdata-button", function() {
//test Confirmed
alert('hello8');
$.getJSON('Test.php', function(data) {
//noresponse
alert('hello5');
$('#showdata').html("item1="+data.item1+" item2="+data.item2+" item3="+data.item3+"");
});
});
});
测试。 php:
< ?php
$items = array(
'item1' => 'I love jquery4u',
'item2' => 'You love jQuery4u',
'item3' => 'We love jQuery4u'
);
header('Content-type: application/json');
echo json_encode($items);
?>
我的预期结果是html中的item1/item2/item3。
无论如何,它在控制台中说了什么? –
检查我的答案,试试看,并告诉我它是否正常工作。 –