javascript 使用 jQuery Ajax POST 和 php 时出现 502 Bad Gateway 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21176308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
502 Bad Gateway Error when using jQuery Ajax POST with php
提问by Sa?o Krajnc
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
部分 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
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}') ");
}
If I post a small amount of data everything is ok, but if I post a large amount I get this error:
如果我发布少量数据一切正常,但如果我发布大量数据,我会收到此错误:
server log:
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?
如何传递这些错误并成功地将数据发送到 mysql?
采纳答案by teynon
Disclaimer: It's been a while since I've done anything in web world.
免责声明:我已经有一段时间没有在网络世界做过任何事情了。
As for an approach to spsc_tech's answer:
至于 spsc_tech 回答的方法:
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>");
}
}
回答by miyasudokoro
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 phpand jquery/javascriptand that should solve your problem.
问题是您的查询中有不允许的字符。例如,我可以清楚地看到该查询字符串中的大量非转义空格。在php和jquery/javascript 中查找 url 编码和解码,这应该可以解决您的问题。