簡體   English   中英

jQuery Ajax方法成功,但沒有收到數據

[英]jQuery Ajax method succeeds, but no data received

我已經花了10多個小時來解決這個問題,並且基本上在整個互聯網上尋找解決方案。 這是一個簡單的jQuery ajax POST方法,我在成功之前已經使用了幾次。 在過去我也有這個問題,但不知何故解決了它。 我傳遞的數據似乎沒問題,在chrome的網絡控制台中它甚至顯示了一個包含所謂數據的成功帖子。 但是,使用.load來獲取該數據始終返回null。 在下面的代碼中,我使用了一個表單,我阻止了默認提交以防止刷新。 一個按鈕觸發sellBook(),它會提示一個表單,然后提交觸發post()。

JS

    function sellBook(i) {
        $('#results').html('');
        title = books[i].title;
        author = books[i].author;
        ISBN = books[i].ISBN;
        publisher = books[i].publisher;
        image = books[i].image;
        year = books[i].year;
        $('#results').html('Listing for ' + books[i].title + ' by ' + books[i].author + '<br><br><form method="post" action="https://localhost/textbookexchange/db2.php" id="sellIt">Edition #: <input type="text" id="edition" name="edition"/><br><br>Price: $<input type="text" id="price" name="price"><br><br>Condition: <input type="text" id="condition" name="condition"><br><br><input type="submit" value="Submit"><br><br></form>');
        getInfo();

        $('#sellIt').submit(function () {
            post();
            return false;
        });
    }

    function post() {
        price = document.getElementsByName('price')[0].value;
        edition = document.getElementsByName('edition')[0].value;
        condition = document.getElementsByName('condition')[0].value;
        var formData = {
            A: title,
            B: author,
            C: ISBN,
            D: publisher,
            E: image,
            F: year,
            G: soldby,
            H: fblink,
            I: price,
            J: edition,
            K: condition
        };

        var url = 'db2.php/'
        jQuery.ajax({
            type: 'POST',
            url: url,
            data: formData,
            success: function (data) {
                alert(data);
                $('#results').load("db2.php/");
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status + " " + thrownError);

            },
        });
    }

我總是返回成功,而不是錯誤,因為我得到了數據警報。

在我的apache錯誤日志中,我得到了這個:

[Thu Aug 13 11:24:15.666854 2015] [:error] [pid 4255] [client 127.0.0.1:50476] PHP注意:未定義的索引:第2行的/var/www/html/textbookexchange/db2.php中的A ,referer: https:// localhost / textbookexchange /

   db2.php(the php file POSTED to)
   <?php
   echo $_POST['A'];
   ?>

我嘗試將其解析為JSON,重置contentType,設置dataType,將crossdomain設置為true,async為false,使用jsonp,使用$ .post,使用更少的數據(只有一個變量),以及一些其他的東西..

下面是網絡請求的屏幕截圖: POST成功,在預覽和表單數據中顯示數據,而get只返回null

load()創建一個單獨的get請求,因此沒有用於php的POST數據。 您需要做的是使用從post請求返回的數據:

success: function(data) {
    alert(data);
    $('#results').html(data); //.load("db2.php/");
},

暫無
暫無

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

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