2013-05-03 116 views
0

我想讓JQuery Ajax工作但沒有獲得成功警報。JQuery Ajax沒有顯示成功警報

下面是代碼:

<script type="text/javascript"> 
    $(document).ready(function(
     $('button').click(function() { 
      $.ajax({ 
       url: 'testing123.php', 
       success: function(){ 
        alert('this worked'); 
       } 
      }); 
      return false; 
     }); 
    )); 
</script> 

<button>Click Here</button> 

...那麼testing123.php文件:

<?php 
    echo 'Hello there'; 
?> 

我有jQuery庫添加了。

當我點擊按鈕,我應該得到一個警告說「這工作」是正確的?

我不明白爲什麼它沒有發生......

任何想法,爲什麼?

+1

檢查開發者工具控制檯任何JavaScript錯誤? – MatthewMcGovern 2013-05-03 10:33:31

+1

是* testing123.php *與這個文件在同一個目錄中? – George 2013-05-03 10:35:41

+0

括號沒有正確使用關閉 – 6339 2013-05-03 10:47:43

回答

2

不正確使用偏食。更正後的代碼:

$(document).ready(function() {  
      $('button').click(function() {  
       $.ajax({ 
        url: 'testing123.php', 
        success: function(){ 
         alert('this worked'); 
        } 
       });  
       return false;  
      });  
    }); 
-1

你需要把哈希與按鈕的ID(「#鍵」)沿單擊

$(document).ready(function() {
$('#button').click(function() {
$.ajax({
url: 'testing123.php',
success: function(){
alert('test echo');
}
});
return false;
});
});

+0

爲什麼'id('#button')'? – 6339 2013-05-03 10:48:46

+0

你需要在按鈕中提供id並在jquery中使用該id dawncode 2013-05-03 10:51:35

+0

-1在這種情況下不需要'id'。他沒有正確關閉括號。 – 6339 2013-05-03 10:53:30