簡體   English   中英

Ajax調用后什么也沒有返回

[英]Nothing returns after ajax call

下面是我的Php文件,在提交按鈕后,我使用ajax從中調用另一個php文件來獲取一些數據:

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    </head>

    <body>

        <form enctype="">
            <button class="refresh"> submit </button>
            <input placeholder="text here" name="roll" >

        </form>

            <script>

                $(document).ready(function(){

                    $('.refresh').click(function(){
                      //  var formData = new FormData($(this)[0]);
                        //event.preventDefault();
                        $.ajax({
                            url:'ajax.php',
                            type:'POST',
                            data:{roll:'rana'},
                            //async:false,
                            success:function(response){
                                alert(response);
                                //document.write(response);
                            },
                            cache:false,
                            contentType:false,
                            processData:false
                        });return fasle;
                    });                               
    });
            </script>


    </body>
    </html>

另一個php文件是:

<?php 
echo 'rana';
echo $_POST["roll"];
?> 

當我寫一些文本並單擊提交按鈕時,輸入字段的文本顯示在url中,並且沒有從另一個php文件中獲取任何數據。 為什么? 誰能給我一些線索或幫助? 謝謝。

單擊按鈕后,表單已提交,您需要使用preventDefault()方法停止該行為。 將腳本替換為以下內容:

<script>

            $(document).ready(function(){

                $('.refresh').click(function(e){
                  e.preventDefault(); // this line will prevent submission of form
                  //  var formData = new FormData($(this)[0]);
                    //event.preventDefault();
                    $.ajax({
                        url:'ajax.php',
                        type:'POST',
                       // data:{roll:'rana'},
                        //async:false,
                        success:function(response){
                            alert(response);
                            //document.write(response);
                        },
                        cache:false,
                        contentType:false,
                        processData:false
                    });return fasle;
                });                               
});
        </script>

您沒有在ajax.php文件中收到任何POST數據,因為沒有任何data ,ajax請求中的data對象為空並已注釋掉。

與您期望的相反,您不能僅通過POST將表單值發送到PHP文件,類似於使用action="file.php method="post"因為在AJAX中,您必須手動“構建” data作為POST請求發送的對象。

運作方式如下:

表格文件:

<form>
    <input placeholder="text here" id="string">
    <input type="submit" class="refresh" value="submit">
</form>

<script>
$(document).ready(function() {
    $('.refresh').click(function(e) {
    e.preventDefault();
        $.ajax(
        {
            url:'ajax.php',
            type:'POST',
            data:{
                string: $('#string').val()
            },
            success:function(response) {
                alert(response);
            }
        }
        );
    });
});
</script>

后端文件:

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

請注意,我是如何通過id而不是name命名輸入的,並通過$('#string').val()檢索值,然后將其分配給通過POST請求發送的string 您應該對表單中的每個輸入字段執行相同的操作。

另外,您不需要這些:

cache:false,
contentType:false,
processData:false

因為原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM