【问题标题】:AJAX call does not post the dataAJAX 调用不发布数据
【发布时间】:2013-07-14 12:37:11
【问题描述】:

我正在使用 Code Igniter 2.1.3 开发一个网站。我正在使用 AJAX 调用将数据发布到我的控制器,但由于某种原因,数据没有被发送。

我在整个应用程序中都使用 AJAX 调用,它们都运行良好,只是不是这个。

我的看法:

<div id="ajaxResult">
<div class="page-header"><h2>Review Comments</h2></div>
<div class="row-fluid">
<div class="span12">
<?php if(count($comments)): ?>
<table class="table">
<thead>
<tr>
<th>Author</th>
<th>Email</th>
<th>IP Address</th>
<th>Date</th>
<th>Comment</th>
<th>Approve</th>
</tr>
</thead>
<tbody>
<?php foreach($comments as $comment): ?>
<tr>
<td>
<?php echo $comment->comment_author; ?>
</td>
<td><?php echo $comment->comment_author_email; ?></td>
<td><?php echo $comment->comment_author_IP; ?></td>
<td>
<?php
$date = new DateTime($comment->comment_date);
$date = $date->format('d/m/Y');
echo $date;
?>
</td>
<td>
<?php echo $comment->comment_content; ?>
</td>
<td>
<?php
$approve = array(
'name' => 'comment_approved',
'class' => 'approve',
'value' => '1',
'data-commentid' => $comment->id
);
echo form_checkbox($approve);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No comments were found for review.</p>
<?php endif; ?>
</div>
</div>
</div>
<script>
$(function()
{
$('.approve').on('click', function()
{
var id = $(this).data('commentid'); alert(id);
$.ajax({
url: "<?php echo site_url('blog/admin/review_comment'); ?>",
type:'POST',
data: { comment_id: id },
success: function(data) { $('#ajaxResult').html(data); } // End of success function of ajax form
}); // End of ajax call
});
});
</script>

我的控制器功能:

public function review_comment()
{
$this->load->helper('form');
$this->load->model('blog_comment_m');
if(isset($_POST['comment_id']))
{
$id = $this->input->post('comment_id');
$data['comment_approved'] = 1;
$this->blog_comment_m->save($data, $id);
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->load->view('admin/review_comments', $this->data);
}
else
{
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->data['subview'] = 'admin/review_comments';
$this->load->view('admin/_layout_main', $this->data);
}
}

我试图提醒 comment_id,它确实给了我一个值 1。在控制器中我做 var_dump($_POST),总是空的。

我也试过if(isset($_POST['comment_id']))。在控制器中它永远不会进入if(isset($_POST['comment_id']) 语句。

为什么会这样?

【问题讨论】:

  • 确定数据是否正在发送的最佳方法是检查network 层。 Firebug addonfirefoxchrome(右键单击 -> 检查元素 -> 网络选项卡),或安装 fiddler fiddler2.com。请确认数据没有通过网络层发送。否则,问题出在其他地方。
  • 您也可以在控制台选项卡中看到发送和获取。
  • 如果我在 firebug 中查看 NET 选项卡,我会看到:状态:发现 302 错误(阅读相关内容,我没有重定向任何东西,不确定为什么会出现此错误。在帖子选项卡中我看到this: 参数comment_id = 1 来源comment_id = 1 所以对我来说一切都很好?还有什么我应该检查的吗?非常感谢
  • 现在你必须检查为什么你得到 302。也许你在.htaccess 中有重定向或者 CI 做了一些重定向——例如登录页面,因为 ajax 没有管理员权限)。获取在 ajax 中使用的 url 并直接在另一个浏览器中测试它(没有 cookie,来自您的页面的会话)。您还可以在命令行中使用curl 来测试该网址。顺便说一句:你的 ajax 代码在哪里?
  • 您好,furas,谢谢。我以管理员身份登录时直接在另一个浏览器中访问了该 url,它运行良好。如果我没有以管理员身份登录,我将无法访问视图。我的 ajax 代码在底部的视图中,您可以在我的问题中看到它。这太令人沮丧了...我的 htaccess 文件:RewriteEngine On RewriteCond $1 !^(index\.php|assets|images|robots\.txt|captcha) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

标签: php ajax post


【解决方案1】:

找到了。

事实上,我开始在表单提交上使用 csrf,对我来说是一个新事物,我没有将这个 url 添加到配置文件中。

添加这个:

$config['csrf_exclude_uris'] = array('blog/admin/review_comment');

问题解决了……

非常感谢大家的帮助。

【讨论】:

  • 如果可以,请点击答案分数下方的绿色大勾号 (✔) 接受此答案。
  • 明天做,今天不让我做... ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 2016-07-30
  • 2020-02-24
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多