【发布时间】:2015-01-11 16:34:15
【问题描述】:
我在使用 jQuery 从一页到另一页发布数据时遇到问题。帖子根本没有提交。我已经使用网络选项卡检查了发布数据,我可以看到没有发送任何表单数据。但我不知道为什么。我按照here 的建议尝试了evt.preventDefault();。但结果是一样的。
那么这是如何工作的。 index.php 将数据发布到 referrer-reason.php,它会更改 div 并运行新查询并在 div 中显示新数据。 #backref 是一个后退按钮,它从 index.php 但在referrer-company.php 中加载相同的代码。目前referrer-reason.php 没有向referrer-company.php 发布数据。但是在将数据从 index.php 发布到 referrer-reason.php 时,同样的代码也可以工作。
它会返回 200 OK 但帖子数据中没有实际内容,如下所示。
我对 jQuery 还很陌生,所以这是我在学习错综复杂的过程中试图保持简单的原因。
还有什么我可以尝试的建议吗?
index.php 上的代码:
$(document).ready(function () {
$('.clickthrough2').click(function () {
companyId = $(this).attr('id');
$.post('./ajax/referrer-reason.php', {
clickthrough: $('#company-'+companyId+' .clickthrough').val(),
ref_date_from2: $('#company-'+companyId+' .ref_date_from2').val(),
ref_date_to2: $('#company-'+companyId+' .ref_date_to2').val()
},
function (data) {
$('#declined2').html(data);
});
});
});
POST DATA 从 index.php 到 referrer-reason.php。
Remote Address:IP:80
Request URL:http://www.domain.com/sections/dash/ajax/referrer-reason.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:65
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:PHPSESSID=****
Host:www.domain.com
Origin:http://www.domain.com
Referer:http://www.domain.com/sections/dash/index.php
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
clickthrough:84
ref_date_from2:1354320000
ref_date_to2:1420934400
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:close
Content-Type:text/html; charset=UTF-8
Date:Sun, 11 Jan 2015 15:46:23 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:Apache/2.2.15 (CentOS)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.4.33
推荐人原因代码
$(document).ready(function () {
$('#backref').click(function () {
companyId = $(this).attr('id');
$.post('./ajax/referrer-company.php', {
clickthrough: $('#company-'+companyId+' .clickthrough').val(),
ref_date_from2: $('#company-'+companyId+' .ref_date_from2').val(),
ref_date_to2: $('#company-'+companyId+' .ref_date_to2').val()
},
function (data) {
$('#declined2').html(data);
});
});
});
将数据从referrer-reason.php 发送到referrer-company.php。
Remote Address:IP:80
Request URL:http://www.domain.com/sections/dash/ajax/referrer-company.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-GB,en-US;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:0
Cookie:PHPSESSID=****
Host:www.domain.com
Origin:http://www.domain.com
Referer:http://www.domain.com/sections/dash/index.php
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36
X-Requested-With:XMLHttpRequest
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:close
Content-Type:text/html; charset=UTF-8
Date:Sun, 11 Jan 2015 15:46:29 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Pragma:no-cache
Server:Apache/2.2.15 (CentOS)
Transfer-Encoding:chunked
X-Powered-By:PHP/5.4.33
编辑:要求提供更多细节:
收集 POST 后:
$selected = $_POST['clickthrough'];
$date_from = $_POST['ref_date_from2'];
$date_to = $_POST['ref_date_to2'];
我明白了:
PHP Notice: Undefined index: clickthrough in /var/www/clients/client1/web51/web/sections/dash/ajax/referrer-company.php on line 35, referer: http://www.domain.com/sections/dash/index.php
PHP Notice: Undefined index: ref_date_from2 in /var/www/clients/client1/web51/web/sections/dash/ajax/referrer-company.php on line 36, referer: http://www.domain.com/sections/dash/index.php
PHP Notice: Undefined index: ref_date_to2 in /var/www/clients/client1/web51/web/sections/dash/ajax/referrer-company.php on line 37, referer: http://www.domain.com/sections/dash/index.php
第二个帖子数据也有:
Content-Length:0
正在被替换的来自referrer-reason.php的代码:
<div id="declined2" class="declined4">
<h4>Declined Quote <br />Reasons</h4>
<div class="statgrid">
<?php
$result = $mysqli->query($quotes_q);
while ($row = $result->fetch_array()) {
?>
<div id="company-<?php echo $row['company_id'];?>">
<input type="hidden" class="ref_date_from2" value="<?php echo $date_from; ?>" />
<input type="hidden" class="ref_date_to2" value="<?php echo $date_to; ?>" />
<input type="hidden" class="clickthrough" value="<?php echo $row['company_id'] ?>" />
<a><div id="<?php echo $row['company_id'];?>" class="clickthrough2 col-5-6"><?php echo $row['decline_reason']; ?></div></a>
<div class="col-1-6"><?php echo $row['company_id_count']; ?></div>
</div>
<?php
}
【问题讨论】:
-
可以看到数据已发送,状态为200。详细说明问题
-
@charlietfl 根据要求添加了额外的细节
-
不知道为什么它不会发送数据。将尝试使用
var companyId隔离变量,这有时会导致问题,并且忽略var是一种非常糟糕的做法。否则,对控制台进行一些日志记录和/或设置断点以查看 clcik 处理程序中发生的情况。也许在响应中没有正确设置值,或者 ID 不正确......如果没有直接访问 DOM 就不确定 -
没想到是这样,只是我注意到的一个坏习惯
-
发生在我们所有人身上。然而,下一次将更好地掌握如何建立一个更易于理解的问题......祝你好运