2015-03-03 81 views
0

這裏是我試圖從php到ajax的數據的簡單代碼。 我只是想獲得一個簡單的數據傳遞迴ajax成功。我已經搜索了這個,但我無法正確地得到它。我的代碼非常簡單。undefined return ajax success

<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script> 
<SCRIPT TYPE="text/javascript"> 
$(document).ready(function(){ 
$("#1").click(function(){ 
$.ajax({ 
      type: "POST", 
      url: "ajax.php", 
      data: { "txt": "try"}, 
      cache: false, 
      success: function(html) 
      {      
       alert(html.mes); 
      } 
      }); 
}); 
}); 
</SCRIPT> 
<pre><button id="1">try</button></pre> 

然後我加載有一個簡單的代碼太

$var['mes'] = 'message'; 
echo json_encode($var); 

及其提醒我 「未定義」 一ajax.php。我知道這是簡單的,但我不能找到它在哪裏我錯了

回答

3

你需要告訴jQuery的腳本運行JSON:

$(document).ready(function(){ 
    $("#1").click(function(){ 
     $.ajax({ 
      type: "POST", 
      url: "ajax.php", 
      data: { "txt": "try"}, 
      dataType: 'json', 
      cache: false, 
      success: function(html) 
      {      
       alert(html.mes); 
      } 
     }); 
    }); 
}); 
+0

謝謝。這是唯一的問題,我對此抱歉。非常感謝先生 – 2015-03-03 03:32:44