使用带有php的jQuery Ajax POST时出现502 Bad Gateway错误

时间:2014-01-17 01:33:16

标签: javascript php jquery mysql ajax

我正在尝试将数据发送到数据库。这是我正在使用的方法:

<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>

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);}

Profile

的一部分
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}') ");

}

如果我发布少量数据一切正常,但如果我发布了大量数据,我会收到此错误:

enter image description here enter image description here enter image description here

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"

如何传递这些错误并成功将数据发送到mysql?

2 个答案:

答案 0 :(得分:1)

免责声明:自从我在网络世界做了一些事以来,已经有一段时间了。

关于spsc_tech答案的方法:

我通常喜欢将数据包装到像这样的对象中:

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>");
    }
}

答案 1 :(得分:0)

问题是您的查询中有不允许的字符。例如,我可以清楚地看到该查询字符串中有大量非转义空格。在phpjquery/javascript中查找网址编码和解码,这应该可以解决您的问题。