duanli0453 2018-07-18 19:24
浏览 113
已采纳

jquery到php发布500内部服务器错误.load

I was googling for a few hours but I'm still stucked with this. I'm trying to send data to a php file with .load jquery function. I'm trying this:

$(document).ready(function() {
        var commentCount = 2;
        $("button").click(function() {
            commentCount = commentCount + 2;
            console.log("Coment Count: " + commentCount);
            $("#comments").load("load-comments.php", {
                'commentNewCount': commentCount
            });
        });
    });

And my php should receive this variable as POST so I can get the information from a MySQL call.

$commentsNewCount = $_POST['commentNewCount'];
$more_comments->bindParam(':limits', $commentsNewCount, PDO::PARAM_INT);
$more_comments->execute();
$more_all_comments = $more_comments->fetchAll();
foreach ($more_all_comments as $key => $rs) {
    echo '<p>';
    echo $rs['author'];
    echo '<br>';
    echo $rs['message'];
    echo '</p>';
}

I checked the console from the developer tool and I get the following error:

POST http://my_url/load-comments.php 500 (Internal Server Error)

I already try to implement a try catch in the .load function but I wasn't be able to get the error message, I printed several console.log as a mode for debugging but I don't know what could be the error.

This is for printing in real time, searched in google but can't find something like this, a lot of people use more complex functions like $.ajax or something like so I'm guessing my error is very simple.

Please, help me with your wisdom.

  • 写回答

3条回答 默认 最新

  • doucigua0278 2018-07-18 19:53
    关注

    You can pass data with load(), I think that the error is in the server side, you should check, debug or use var_dump in php and maybe you can use postman and send a post request with the param commentNewCount in the body. ref: http://api.jquery.com/load/

    The load() method send a GET request if you only pass the url, but if you pass a params object it send a POST request, check https://github.com/jquery/jquery/blob/3d732cca6b5076a9d13eee98e2b075b37384cd91/src/ajax/load.js#L34

    For example:

    $( '#comments' ).load( 'load-comments.php', 
      { commentNewCount: commentCount },
      function() {
        console.log('server return');
      }
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低