2015-11-29 35 views
-1

我对这个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。

+0

无论如何,它在控制台中说了什么? –

+0

检查我的答案,试试看,并告诉我它是否正常工作。 –

回答

0

php之前有一个(space)字符:

< ?php 

将其更改为:<?php

不是,删除此:

header('Content-type: application/json'); 

,或是更换:

header('Content-type: text/javascript'); 

我不知道为什么,但它的作品。

+0

我做了这些改变,但没有发生任何不同,我期待通过项目1到3回到网页。 – bawg2005

+0

PHP和可见文件都在同一个文件夹中吗? –

+0

控制台@ bawg2005中显示了什么? –