2014-03-03 84 views
0

我一直在尋找約2-4小時的計算器,但找不到我的解決方案。Ajax jQuery非法調用

我的代碼應該提醒數據值。但只給出了:非法調用

代碼:(JavaScript)的

function getData() { 

$(document).ready(function() { 

    var withdraw = $('#withdraw').val(); 
    var deposit = $('#deposit').val(); 
}); 
$.ajax({ 
     type: "POST", 
     url: "../ajax/house_info.php", 
     data: { 
       'withdraw' : withdraw, 
       'deposit' : deposit 
     }, 
     dataType: "json", 

     success: function(data){ 
      console.log(data); 
      alert(data.test); 
     }, 

     error: function(jqXHR, textStatus, errorThrown) { 
      console.log(jqXHR.responseText); 
     } 
    }); 

return false; 
} 

代碼:(HTML)

  <form action='#' method='something'> 
     Withdraw: <input type='text' id='withdraw' /> 
     Deposit: <input type='text' id='deposit' /> 
     <input type='submit' onClick='getData()' /> 
     </form> 

代碼:(PHP(AJAX進行文件)

<?php 
if(isset($_POST['deposit'])) 
{ 
    $output["test"] = "Test"; 
    echo json_encode = $output; 
} 
?> 

回答

0

var withdraw有局部範圍,所以在ajax中使用時會變爲null,只需在$ ajax調用中使用$('#withdraw')。val(); 從表格中刪除動作='#'和方法='東西'

function getData(){ 
    var withdraw = $('#withdraw').val(); 
    var deposit = $('#deposit').val(); 
    $.ajax({ 
    type: "POST", 
    url: "../ajax/house_info.php", // type full url 
    data: 'withdraw='+withdraw+'&deposit='+deposit, 
    success: function(data){ 
     console.log(data); 
     alert(data.test); 
     } 
    }); 
    } 
+0

不修復它,D :. – user2999556

+0

我編輯了答案。這個代碼必須工作。 – gzix