无法使用$ .ajax({data})向PHP发送变量

时间:2013-07-30 17:54:51

标签: php jquery slim

我尝试过这些 JS 代码组合:

var aion_settings = [];

function aion_save_settings(){

    //Users on the Site Frontend
    $('.setting-site-users').each(function(){
        aion_settings[$(this).prop('id')]=$(this).is(':checked');
    });

    console.log(aion_settings);

    $.ajax({
        method:'post',
        url:'/save_settings',
        dataType:'json',
        data:{
            settings: function(){
                return aion_settings;
            },
            other_data: 'Other Data'
        },
        success:function(result){
            console.log(result);
        }
    });
}

和...

function aion_save_settings(){

    var aion_settings = [];

    //Users on the Site Frontend
    $('.setting-site-users').each(function(){
        aion_settings[$(this).prop('id')]=$(this).is(':checked');
    });

    console.log(aion_settings);

    $.ajax({
        method:'post',
        url:'/save_settings',
        dataType:'json',
        data:{
            settings: aion_settings,
            other_data: 'Other Data'
        },
        success:function(result){
            console.log(result);
        }
    });
}

..和

var aion_settings = [];

function aion_save_settings(){

    //Users on the Site Frontend
    $('.setting-site-users').each(function(){
        aion_settings[$(this).prop('id')]=$(this).is(':checked');
    });

    console.log(aion_settings);

    $.ajax({
        method:'post',
        url:'/save_settings',
        dataType:'json',
        data:{
            settings: aion_settings,
            other_data: 'Other Data'
        },
        success:function(result){
            console.log(result);
        }
    });
}

..和这些组合:

$.ajax({
    method:'post',
    url:'/save_settings',
    dataType:'json',
    data:aion_settings,
    success:function(result){
        console.log(result);
    }
});

在这个JQuery页面上,它甚至有这个例子:

var xmlDocument = [create xml document];
var xmlRequest = $.ajax({
  url: "page.php",
  processData: false,
  data: xmlDocument
});

xmlRequest.done(handleResponse);

在接收方,我有 PHP 代码:

$app->post('/save_settings',function() use ($app){

    $aion_settings=$app->request()->post();

    var_dump($aion_settings);

    //Save the aion_settings
    if(aion_logged_in_user_super()){

        global $aion_db;
        if(is_array($aion_settings)) foreach($aion_settings as $setting_key => $setting){

            //Get the setting's ID
            $current_setting = array();
            $current_setting = $aion_db->queryFirstRow("SELECT id FROM settings WHERE setting_key=%s",$setting_key);

            if(!isset($current_setting['id'])) $current_setting['id']=NULL;

            $aion_db->insertUpdate('settings',array(
                'id'=>$current_setting['id'],
                'setting_key'=>$setting_key,
                'value'=>serialize($setting)
            ));
        }
    }
});

aion_settings请求发送之前,$.ajax已正确设置。但是,对象settings没有在PHP端被捕获,但other_data被捕获了?下面的最后一行显示了通过console.log准备好的对象,但未通过$.ajax设置。我很难过,有什么帮助吗?

Chrome DEV tools showing PHP and JS side before var is sent

Screenshot of the code

2 个答案:

答案 0 :(得分:0)

$.ajax函数的data参数应采用以下格式:

data: {
    'settings': aion_settings,
    'other_data': 'Other Data'
},

答案 1 :(得分:0)

这就是我修复它的方法:

JS方面:

function aion_save_settings(){

    var aion_settings = new Object();   

    //Users on the Site Frontend
    $('.setting-site-users').each(function(){
        aion_settings[$(this).prop('id')]=$(this).is(':checked');
    });

    console.log(aion_settings);

    $.ajax({
        method:'post',
        url:'/save_settings',
        dataType:'json',
        data:aion_settings,
        success:function(result){
            console.log(result);
        }
    });
}

PHP方面:

$app->post('/save_settings',function() use ($app){

    $aion_settings=$app->request()->post(); 

    var_dump($aion_settings);

    //Save the aion_settings
    if(aion_logged_in_user_super()){

        global $aion_db;
        if(is_array($aion_settings)) foreach($aion_settings as $setting_key => $setting){

            //Get the setting's ID
            $current_setting = array();
            $current_setting = $aion_db->queryFirstRow("SELECT id FROM settings WHERE setting_key=%s",$setting_key);

            if(!isset($current_setting['id'])) $current_setting['id']=NULL;

            $aion_db->insertUpdate('settings',array(
                'id'=>$current_setting['id'],
                'setting_key'=>$setting_key,
                'value'=>serialize($setting)
            ));
        }
    }
});

请与我上面的问题进行比较。

enter image description here

enter image description here