Ajax jQuery非法调用

时间:2014-03-03 15:13:27

标签: javascript php jquery ajax json

我一直在stackoverflow上搜索大约2-4个小时但找不到我的解决方案。

我的代码应提醒数据值。但它只给出:非法调用

代码:(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 proceed file)

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

1 个答案:

答案 0 :(得分:0)

var withdraw具有本地范围。因此在ajax中使用时它将变为null。只需使用$('#withdraw')。val();在ajax电话中。 从表单

中删除action ='#'和method ='something'
  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);
       }
    }); 
  }