微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – jQuery Ajax方法成功,但没有收到数据

我已经花了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 ,参考文献:https://localhost/textbookexchange/

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

我尝试将其解析为JSON,重置contentType,设置dataType,将crossdomain设置为true,async为false,使用jsonp,使用$.post,使用更少的数据(只有一个变量),以及一些其他的东西..

下面是网络请求的屏幕截图:

The POST succeeds, showing data in preview and form data, while the get only ever returns null


解决方法:

load()创建一个单独的get请求,因此没有用于PHP的POST数据.您需要做的是使用从post请求返回的数据:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐