Jquery Ajax POST方法不起作用,返回未定义的值

时间:2016-04-20 02:21:06

标签: javascript php jquery ajax

POST方法返回未定义值,但GET方法正常工作。我甚至试过$.post(url, data, success);<< $.post更糟糕的是GET和POST无法正常工作。

<input type="submit" value="submit"  id="btnUpload">


$('#btnUpload').click(function(){       
$.ajax({  
    type: 'POST',  
    url: 'ajax.php',
    data: { 'action': 'scan_email' },
    success: function(response) {
        alert(response);
        }
    });

ajax.php

<?php echo $_POST['action'];?>

2 个答案:

答案 0 :(得分:1)

使用method代替type

<强>代码:

<input type="submit" value="submit"  id="btnUpload">

$('#btnUpload').click(function(){       
    $.ajax({  
    method: 'POST',  
    url: 'ajax.php',
    data: { 'action': 'scan_email' },
    success: function(response) {
       alert(response);
    }
});

答案 1 :(得分:0)

添加了jquery库

<input type="button" value="submit" id="btnUpload">

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 

    $('#btnUpload').on('click', function(){ 

        $.ajax({
                type: "POST",  
                url: "login.php",  
                data: { 'action':'scan_email'},
                success: function(theResponse) {

                    // Output from ajax.php
                    alert(theResponse); // comment this

                    }  
            });
    });
});                 
</script>