duanli0453 2018-07-18 19:24
浏览 113


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_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');
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 R语言单细胞测序 对seurat对象进行标准化时出现问题
  • ¥15 Anaconda navigator updateor一直更新
  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT