我尝试了很多次来调试这段代码,但它似乎根本不起作用。我在我的应用程序中使用选定的 jquery。
这是我的代码:
$(document).ready(function(){
var status = [];
var method = $(this).attr('data-method');
var config = {
'.chosen-select' : {},
'.chosen-select-deselect' : {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
'.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
'.chosen-select-width' : {width:"95%"}
}
for (var selector in config) {
$(selector).chosen(config[selector]);
}
$("#test").chosen().change(function(e, params){
console.clear()
$("#test :selected").each(function(i,selected)
{
status[i] = $.trim($(selected).text());
})
console.log(status);
var new_var = JSON.stringify(status);
$.ajax({
type: "POST",
url: "<?php echo site_url('request/result') ?>",
data: { data: new_var }
}).done(function(data) {
alert( "Data Send:");
}).fail(function() {
alert( "Data Not Sent" );
});
e.preventDefault();
}); });
new_var 保存数组值,但我无法将其值发布到我的 Controller 。
Controller :request.php
public function result(){
echo json_encode($status);
die();
}
我正在 firefox 上的网络选项卡上调试它,我可以看到参数上的值,但在响应选项卡上它返回NULL我似乎无法解决问题近一周。我不知道如何解决它,因为我是 AJAX 新手
最佳答案
您需要从 $_POST 数组中读取:
public function result(){
// Your variable is here:
var_dump($_POST['data']);
// echo json_encode($_POST['data']);
die();
}
请记得清理$_POST,例如。 G。使用filter_input
关于javascript - 在 PHP 中的 AJAX post 上获取空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41690499/