我花了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错误日志中,我得到了以下内容:
清华8月13日11:24:15.666854 2015年pid 4255PHP通知:未定义索引:a /var/www/html/textbookexchange/db2.php,第2行,referer:https://localhost/textbookexchange/
db2.php(the php file POSTED to)
<?php
echo $_POST['A'];
?>我尝试将其解析为JSON,重置contentType,设置dataType,将横切域设置为true,异步设置为false,使用jsonp,使用$.post,使用较少的数据(只使用一个变量),以及其他一些事情。
以下是网络请求的截图:

发布于 2015-08-13 15:47:13
load()会创建一个单独的get请求,因此没有POST数据供php使用。您需要做的是使用从post请求返回的数据:
success: function(data) {
alert(data);
$('#results').html(data); //.load("db2.php/");
},https://stackoverflow.com/questions/31992880
复制相似问题