未使用ajax和php传递json值
JS代码:未使用ajax和php传递json值,php,jquery,ajax,json,Php,Jquery,Ajax,Json,JS代码: <script type="text/javascript"> $('input:checkbox[name=department]').click(function(){ var message = []; $(':checkbox:checked').each(function(i){ message.push($(this).val()); }); var message1=JSON.stringify({"message":message
<script type="text/javascript">
$('input:checkbox[name=department]').click(function(){
var message = [];
$(':checkbox:checked').each(function(i){
message.push($(this).val());
});
var message1=JSON.stringify({"message":message});
$.ajax({
url:"hi.php",
type:"POST",
data:{'message':message1},
dataType:"json",
//contentType: "application/json;charset=utf-8",
success: function( result ) {
alert(result);
// $('#second').html(result);
},
error: function(error){
console.log("Error:");
console.log(error);
}
});
})
$arr_sir = json_decode($sir_json, true); // Decode the JSON string and turnit into an Array
// Returns the Array format, obtained from the JSON string
echo '<pre>';
var_export($arr_sir);
echo '</pre>';
}
?>
$(“输入:复选框[name=department]”)。单击(函数(){
var消息=[];
$(':复选框:选中')。每个(函数(i){
message.push($(this.val());
});
var message1=JSON.stringify({“message”:message});
$.ajax({
网址:“hi.php”,
类型:“POST”,
数据:{'message':message1},
数据类型:“json”,
//contentType:“应用程序/json;字符集=utf-8”,
成功:功能(结果){
警报(结果);
//$('#second').html(结果);
},
错误:函数(错误){
日志(“错误:”);
console.log(错误);
}
});
})
PHP代码:
$arr_sir = json_decode($sir_json, true); // Decode the JSON string and turnit into an Array
// Returns the Array format, obtained from the JSON string
echo '<pre>';
var_export($arr_sir);
echo '</pre>';
}
?>
$arr\u sir=json\u decode($sir\u json,true);//解码JSON字符串并将其转换为数组
//返回从JSON字符串获取的数组格式
回声';
var_出口($arr_sir);
回声';
}
?>
错误在哪里??我没有得到输出,每次它弹出空值。我将变量转换为json格式,然后在ajax中,我将它像下面这样传递给我的server.php文件数据:{'message':message}
我得到的响应为空。您没有从服务器端脚本返回任何有效的
JSON
。您应该在.php文件的开头添加var\u dump($\u POST),然后查看输出结果。另外,var message1=JSON.stringify({“message”:message})之后的console.log(message1);在调试中应该会有帮助。你不需要json_解码$\u POST['message']
我都做了。它正在直接转换为json文件。但在服务器文件中,它显示为null。这就是问题所在。先生,你能告诉我哪里做错了吗。我的json是:{“message”:[“1”,“2”]}这是传递给脚本的json。您的PHP脚本没有返回任何JSON。