2017-03-02 16 views
1

我正在運行一個非常簡單的Jquery.Ajax調用作爲測試。代碼應該像編程類中的例子一樣工作。但是,在我的Windows XAMPP中,我重新獲得警報的是ajax.php格式的html格式的代碼...雖然從Ajax查詢中獲得了成功的200個代碼。我不知道什麼是錯的。有沒有人有什麼建議?JQuery.Ajax成功,但只是作爲文本返回服務器PHP代碼

Ajax.html

<!DOCTYPE html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
$(document).ready(function() { 
    // Handler for .ready() called. 
    useJQuery(); 
}); 

function useJQuery() { 
    $.ajax({ 
     type: 'POST', 
     url: "ajax.php", 
     data: { i_want_text:'yes' }, 
     success: function (response) { alert(response); }, 
    }); 
} 

</script> 
</head> 
<body> 
</body> 
</html> 

Ajax.php

<? 
    if ($_POST["i_want_text"]) { 
     print "text received with value of " . $_POST["i_want_text"]; 
    } 
?> 
+0

你的意思,你只看到Ajax.php文件的未來的全部內容在警報中?當您的PHP文件中沒有HTML時,不確定「HTML格式」是什麼意思。 – nnnnnn

+1

在ajax.php中更改開頭<?到<?php –

+0

'<?'被稱爲短標籤。這些短標籤的解釋可以在'php.ini'文件中關閉。因此,使用php開始標籤的<?php'形式總是比較安全的。如果它們被關閉,那麼腳本不會被解釋爲php代碼,而是像普通文本一樣被返回給調用者 – RiggsFolly

回答

1

從PHP文件http://php.net/manual/en/language.basic-syntax.phptags.php

PHP allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).` 

so In ajax.php change <? to <?php 
相關問題