最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - 502 Bad Gateway Error when using jQuery Ajax POST with php - Stack Overflow

programmeradmin5浏览0评论

I am trying to send data to a database. Here is the method I am using:

<script>

function add_new_activity(id) {

    var act_type = $("#add_new_activity #act_type").val();
    var act_muscles = $("#add_new_activity #multi").val();
    var act_location = $("#add_new_activity #act_location").val();
    var act_intensity = $("#add_new_activity #act_intensity").val();
    var act_length = $("#add_new_activity #timepicker").val();
    var act_content = $("#add_new_activity #textarea").val();                                                           
    if(act_type != "" && act_muscles != "" && act_location != ""  && act_intensity != "" && act_content != ""){
        $.ajax({
            type: "POST",
            url: "data.php",
            data: "page=profile&add_new_activity=<?php echo $user->user_id; ?>&act_type="+act_type+"&act_muscles="+act_muscles+"&act_location="+act_location+"&act_intensity="+act_intensity+"&act_length="+act_length+"&act_content="+act_content,
            success: function(data){
               location.reload();
            },
            beforeSend: function(){
                $("#add_new_activity #add_act_button").html("<?php echo $translate->_('adding'); ?>...");
            }
        });
    } else {
        $("#add_new_activity #message").html("<div class='alert alert-error'><button type='button' class='close' data-dismiss='alert'></button><?php echo $translate->_('required_field'); ?></div>");
    }
}
</script>

part of data.php

if(isset($_POST['add_new_activity'])){
    $user_id = $_POST['add_new_activity'];
    $act_type = $_POST['act_type'];
    $act_muscles = $_POST['act_muscles'];
    $act_location = $_POST['act_location'];
    $act_intensity = $_POST['act_intensity'];
    $act_length = $_POST['act_length'];
    $act_content = $_POST['act_content'];                                                                                                                                           
    Profile::add_new_activity($user_id, $act_type, $act_muscles, $act_location, $act_intensity, $act_length, $act_content);}

part of Profile class

public static function add_new_activity($user_id, $act_type, $act_muscles, $act_location, $act_intensity, $act_length, $act_content,$user_id=""){
    global $database;
    global $translate;

    $datetime = strftime("%Y-%m-%d %H:%M:%S", time());
    $database->query("INSERT INTO activities (id,type,body,intensity,location,content,length,posted,user_id) VALUES ('','{$act_type}','{$act_muscles}','{$act_intensity}','{$act_location}','{$act_content}','{$act_length}','{$datetime}','{$user_id}') ");

}

If I post a small amount of data everything is ok, but if I post a large amount I get this error:

server log:

ip - - [17/Jan/2014:00:02:00 +0100] "POST /fitstats/data.php HTTP/1.1" 200 537 ".php?lang=en&username=some1ell" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"

How can I pass these error and successfully send data to mysql?

I am trying to send data to a database. Here is the method I am using:

<script>

function add_new_activity(id) {

    var act_type = $("#add_new_activity #act_type").val();
    var act_muscles = $("#add_new_activity #multi").val();
    var act_location = $("#add_new_activity #act_location").val();
    var act_intensity = $("#add_new_activity #act_intensity").val();
    var act_length = $("#add_new_activity #timepicker").val();
    var act_content = $("#add_new_activity #textarea").val();                                                           
    if(act_type != "" && act_muscles != "" && act_location != ""  && act_intensity != "" && act_content != ""){
        $.ajax({
            type: "POST",
            url: "data.php",
            data: "page=profile&add_new_activity=<?php echo $user->user_id; ?>&act_type="+act_type+"&act_muscles="+act_muscles+"&act_location="+act_location+"&act_intensity="+act_intensity+"&act_length="+act_length+"&act_content="+act_content,
            success: function(data){
               location.reload();
            },
            beforeSend: function(){
                $("#add_new_activity #add_act_button").html("<?php echo $translate->_('adding'); ?>...");
            }
        });
    } else {
        $("#add_new_activity #message").html("<div class='alert alert-error'><button type='button' class='close' data-dismiss='alert'></button><?php echo $translate->_('required_field'); ?></div>");
    }
}
</script>

part of data.php

if(isset($_POST['add_new_activity'])){
    $user_id = $_POST['add_new_activity'];
    $act_type = $_POST['act_type'];
    $act_muscles = $_POST['act_muscles'];
    $act_location = $_POST['act_location'];
    $act_intensity = $_POST['act_intensity'];
    $act_length = $_POST['act_length'];
    $act_content = $_POST['act_content'];                                                                                                                                           
    Profile::add_new_activity($user_id, $act_type, $act_muscles, $act_location, $act_intensity, $act_length, $act_content);}

part of Profile class

public static function add_new_activity($user_id, $act_type, $act_muscles, $act_location, $act_intensity, $act_length, $act_content,$user_id=""){
    global $database;
    global $translate;

    $datetime = strftime("%Y-%m-%d %H:%M:%S", time());
    $database->query("INSERT INTO activities (id,type,body,intensity,location,content,length,posted,user_id) VALUES ('','{$act_type}','{$act_muscles}','{$act_intensity}','{$act_location}','{$act_content}','{$act_length}','{$datetime}','{$user_id}') ");

}

If I post a small amount of data everything is ok, but if I post a large amount I get this error:

server log:

ip - - [17/Jan/2014:00:02:00 +0100] "POST /fitstats/data.php HTTP/1.1" 200 537 "http://ssbox.si/fitstats/profile.php?lang=en&username=some1ell" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36"

How can I pass these error and successfully send data to mysql?

Share Improve this question edited Jan 17, 2014 at 2:12 teynon 8,38810 gold badges72 silver badges109 bronze badges asked Jan 17, 2014 at 1:33 Sašo KrajncSašo Krajnc 1332 gold badges3 silver badges11 bronze badges 4
  • What do your server error logs say – jszobody Commented Jan 17, 2014 at 1:34
  • I updated my question with server log that I am able to access – Sašo Krajnc Commented Jan 17, 2014 at 1:54
  • That looks like your apache access.log, you need the error.log – jszobody Commented Jan 17, 2014 at 1:57
  • I can't get it :/ I can only access to access.log – Sašo Krajnc Commented Jan 17, 2014 at 2:04
Add a ment  | 

2 Answers 2

Reset to default 1

Disclaimer: It's been a while since I've done anything in web world.

As for an approach to spsc_tech's answer:

I generally like to wrap my data into an object like so:

function add_new_activity(id) {
    var act_type = $("#add_new_activity #act_type").val();
    var act_muscles = $("#add_new_activity #multi").val();
    var act_location = $("#add_new_activity #act_location").val();
    var act_intensity = $("#add_new_activity #act_intensity").val();
    var act_length = $("#add_new_activity #timepicker").val();
    var act_content = $("#add_new_activity #textarea").val();                                                           
    if(act_type != "" && act_muscles != "" && act_location != ""  && act_intensity != "" && act_content != ""){
        var theData = {};
        theData['page'] = 'profile';
        theData['add_new_activity'] = '<?php echo $user->user_id;?>';
        theData['act_type'] = act_type;
        theData['act_muscles'] = act_muscles;
        theData['act_location'] = act_location;
        theData['act_intensity'] = act_intensity;
        theData['act_length'] = act_length;
        theData['act_content'] = act_content;

        $.ajax({
            type: "POST",
            url: "data.php",
            data: theData,
            success: function(result){
               location.reload();
            },
            beforeSend: function(){
                $("#add_new_activity #add_act_button").html("<?php echo $translate->_('adding'); ?>...");
            }
        });
    } else {
    $("#add_new_activity #message").html("<div class='alert alert-error'><button type='button' class='close' data-dismiss='alert'></button><?php echo $translate->_('required_field'); ?></div>");
    }
}

The problem is that you have characters in your query that are not allowed. I can clearly see a ton of non-escaped spaces in that query string, for example. Look up url encoding and decoding in both php and jquery/javascript and that should solve your problem.

发布评论

评论列表(0)

  1. 暂无评论