通过ajax发送php代码不起作用

通过ajax发送php代码不起作用,php,jquery,ajax,http-status-code-403,Php,Jquery,Ajax,Http Status Code 403,我试图用jquery发出ajax请求,但每次发送php函数时都会收到403个禁止的错误。我需要的一个例子是这个stackoverflow注释输入,它也接受源代码 我所做的: PHP class Error { public function __construct() { // etc } public function comment($error_id, $content) { if(!$this->user->loged) retur

我试图用jquery发出ajax请求,但每次发送php函数时都会收到403个禁止的错误。我需要的一个例子是这个stackoverflow注释输入,它也接受源代码

我所做的:

PHP

class Error {
  public function __construct() {
    // etc
  }

  public function comment($error_id, $content) {

    if(!$this->user->loged)
      return false;

    $error_id = $this->mysqli->real_escape_string($error_id);
    if(!is_numeric($error_id))
      return false;

    $this->mysqli->query("INSERT INTO comments 
                          VALUES (NULL, '" . $error_id . "', '" . $this->user->user_id . "', '" . $content . "', NULL)");
    return $this->mysqli->insert_id;

  }
}
其中,
$content
使用
escapeString
函数在
Common
类中转义:

public function escapeString($text) {
  return htmlspecialchars($this->mysqli->real_escape_string($text));
}
我的JS是:

$('div.left-content').on('click','form.comment input[type=button]',function(){

        $but = $(this);

        if(!erori.user.loged) {

            showTooltip($but, 'Trebuie să fii autentificat pentru a putea comenta!');

            return false;
        }

        if($but.prev('textarea').val() == $but.prev('textarea').data("text") || $but.prev('textarea').val().trim() == '') {

            showTooltip($but, 'Completează câmpul de text pentru a putea trimite comentariul!');

            return false;
        }

        if($but.prev('textarea').hasClass('disabled'))
            return false;

        $but.attr('disabled', 'disabled');
        $but.prev('textarea').addClass('disabled');

        $.post(
            $but.parent().attr('action'),
            $but.parent().serialize(),
            function(data){
                if(data.content) {
                    $('<div class="comment"></div>').html(data.content).insertBefore($but.parent().parent());
                    if($but.prev('textarea').hasClass('disabled'))
                        $but.prev('textarea').val('').trigger('blur').animate({height: '20px'}, 300);
                    $but.prev('textarea').removeClass('disabled');
                }

                $but.removeAttr('disabled');

            },
            'json')
        .fail(function(xhr, textStatus, errorThrown) {
            if(errorThrown == 'Forbidden') {
                showTooltip($but, 'Comentariul nu a putut fi trimis!<br />Dacă vrei să trimiți sursă de cod folosește <strong>` cod sursă `</strong>!');

                $but.prev('textarea').removeClass('disabled');
                $but.removeAttr('disabled');

                return false;
            }
        })                                                         
    }); 
$error = new Error($mysqli, $user);

$content = $common->escapeString($_POST['error_comment']);

$comment_id = $error->comment($_POST['error_id'], $content);
如何在将源代码发送到服务器之前对其进行转义,以避免返回此403禁止的错误


我想说的是,如果我试图评论,例如:
这是我对
的评论,服务器抛出403错误代码

在发送代码之前,您无法真正过滤代码,因为用户可以直接关闭javascript并将代码发送给您,跳过任何客户端过滤


403错误-这是一个“权限”错误,因此您需要修复php文件的标题或权限。

right。。观点很好。。那么服务器端的解决方案是什么?对于不评估代码…为什么不展开“escapeString($text)”并放置您需要的其他过滤器?这些过滤器是什么?:)这就是我需要的。。但我不知道这些过滤器实际上,你可以做str_替换,如果你写评论没有php代码,一切都好吗?是的。。一切都好。。即使我写过js、css或一些php代码。。一切都好。。但是当尝试发送一些php函数时,它返回403错误