2011-07-16 106 views
1

我有這樣的代碼爲什麼這個jQuery不會觸發.ajax()請求?

// jquery 
$(document).ready(function() { 
    $('#update_point').live("change", function() { 
     var point_val = $('#update_point').val(); 
     $.ajax({ 
      url: 'send/user/update_point.php', 
      type: 'get', 
      data: 'point='+point_val, 
      dataType: 'json', 
      success: function(data){ 
       alert(data); 
       $('#update_point_result').html(data); 
      } 
     }); 
     return false; 
    }); 
}); 

爲什麼代碼不被炒魷魚嗎?但如果我刪除dataType,代碼的作品。爲什麼?

// jquery 
$(document).ready(function() { 
    $('#update_point').live("change", function() { 
     var point_val = $('#update_point').val(); 
     $.ajax({ 
      url: 'send/user/update_point.php', 
      type: 'get', 
      data: 'point='+point_val, 
      success: function(data){ 
       alert(data); 
       $('#update_point_result').html(data); 
      } 
     }); 
     return false; 
    }); 
}); 

任何幫助將appreaciate它! 謝謝!


編輯

update_point.php包含此代碼。

<?php 
require "../../inc/json.php"; 
if($_GET){ 
    foreach($_GET as $key=>$val){ 
     $respon[$key] = $val; 
    } 
} 

// initialitation json object 

$json = new Json(); 
echo $json->encode($respon); 
die(); 
?> 

回答

5

$.ajax喜歡被嚴重形成JSON時靜默失敗。使用諸如jsonlint之類的工具檢查服務器中的JSON格式是否正確。

可以使用.error callback檢查被拋出的錯誤類型:

$.ajax({ 
     url: 'send/user/update_point.php', 
     type: 'get', 
     data: 'point='+point_val, 
     success: function(data){ 
      alert(data); 
      $('#update_point_result').html(data); 
     }, 
     error: function(jqXHR, textStatus, errorThrown) { 
      alert(errorThrown); // likely 'parseError' 
     } 
}); 
+0

返回我的警報「的翻譯:」爲什麼? –

+0

我不確定。很確定你的問題是形成糟糕的JSON。正如我所說的,通過JSONlint來找出問題所在。 – karim79

相關問題