doutan1970 2014-01-17 01:33
浏览 297
已采纳

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

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:

enter image description hereenter image description hereenter 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"

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

  • 写回答

2条回答 默认 最新

  • douchuntang2827 2014-01-17 02:06
    关注

    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>");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘