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 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');
      }
    );
    
    点赞 评论 复制链接分享
  • duanhao1004 duanhao1004 2018-07-18 19:43

    I'm unsure if your jQuery in anyway send a POST and not GET request to the PHP file, therefor try to do this first in your PHP file:

    if(!$_POST['commentNewCount']){
      die("No post request sent");
    }elseif(isset($_GET['commentNewCount'])){
      die('It was a GET request :)');
    }
    
    点赞 评论 复制链接分享
  • dongyidao1461 dongyidao1461 2018-07-19 17:10

    First of all thanks to all of you. With all your answers I manage to solve my problem, now I feel like I should tell what I did (most based in your answers) and conclude that my error was because a very simple thing.

    In the end I did to versions of my front file (the one with the jQuery code). As you mention in some answers I check the server side, I tried to get the response of server with console.log and to achieve this I also hardcode my server side php file like this:

    $commentsNewCount = $_POST['commentNewCount'];
    $more_comments->bindParam(':limits', 2, PDO::PARAM_INT);
    $more_comments->execute();
    

    And I get an error about I can't reference a parameter so I changed to this:

    $more_comments->bindValue(':limits', 2, PDO::PARAM_INT);
    

    That fixed the last error but now I had the problem with the 500 POST error, first I tried to print with console.log and what i get was an empty array [] so I run in console I run in console my php file. I found the error there! My variable $commentsNewCount had been taken as a string so in the end I just changed this line to this:

    $more_comments->bindValue(':limits', intval($commentsNewCount), PDO::PARAM_INT);
    

    And wow! Now it's working. Following advice I write two versions so I can learn, one was the one I proposed first and the other with .ajax; look:

    The original:

    <script type="text/javascript">
        //jQuery code!
        $(document).ready(function() {
            var commentCount = 2;
            $("button").click(function() {
                commentCount += 2;
                //console.log("Coment Count: " + commentCount);
                $("#comments").load("load-comments.php", {
                    'commentNewCount': commentCount 
                });
            });
        });
    </script>
    

    The one with .ajax

    <script type="text/javascript">
        //jQuery code!
        $(document).ready(function() {
            var commentCount = 2;
            $("button").click(function() {
                commentCount += 2;
                $.ajax({                        
                  type: "POST",                 
                  url: "load-comments.php",   
                  data:"commentNewCount=" + commentCount,
                  success: function(data)            
                  {
                    console.log(data);
                  }
                });
            });
        });
    </script>
    

    Thank you again for all your advice. And I hope my answer is well redacted as well.

    点赞 评论 复制链接分享

相关推荐