通过Ajax发布时,PHP $ _POST为空

时间:2015-05-29 21:37:51

标签: php jquery ajax

我正在开发一个CodeIgniter Web应用程序,在将图像数据URL作为ajax请求的一部分发布时,我似乎遇到了一个非常奇怪的问题。使用名为JCrop的jQuery插件获取数据URI。特点是下面的代码在localhost上运行得很好,但是当我在共享托管服务器上运行相同的代码时,POST主体是神秘的空。

ajax

$('#save-settings').click(function() {
    var user = {
        id: $('#id').val(),
        email: $('#email').val(),
        phone: $('#phone').val(),
        settings: {
            profile_pic: $('#image_output').attr('src') //returns a really long data URI string, eg. data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAY....
            notifications: false
        }
    };

    $.ajax({
        url: '/api/save_settings',
        type: 'POST',
        dataType: 'json',
        data: {
            type: 'SETTINGS_USER',
            settings: JSON.stringify(user),
        },
        success: function(response) {
            console.log(response);
        },
    });
});

PHP

public function save_settings() {
    try {
        /****************************************
        * The problem lies below. It runs fine
        * on localhost, but generates an error
        * "Notice: Undefined index type" on the
        * shared hosting environment
        ****************************************/
        $type = $_POST['type']; //Not sure why it's undefined

        error_log(print_r($_POST, true)); //<--- prints an empty array in the production environment, but correctly prints  the indexes on localhost
        if($type == "SETTINGS_ADMIN") {
            //do admin related stuff
        } else if($type == "SETTINGS_USER") {
            //do user related stuff
            //use GD related functions to create and save jpeg image from data URI string
        }
    } catch(Exception $e) {
        error_log("Something went wrong");
    }
}

查看Firebug控制台,它显示已设置type变量,但settings部分缺少Parameters的字符串化版本,尽管它显示在{{{ 1}}。此外,Firebug报告POSTed请求大小约为150KB(大部分是真正长的数据URI字符串)。

firebug consolehttp://i.imgur.com/uK3YiyI.png

由于这在localost上运行良好,我尝试比较一些关键的php.ini设置,如Sourcepost_max_sizeupload_max_filesize,它们在8M,16M和1000时相当高分别

获取ajax请求以在共享托管环境上工作的唯一方法是从JS对象中删除真正长的数据URI字符串。我不确定是什么阻止了代码在生产环境中工作,我现在已经超过6个小时了![/ p>

编辑:在尝试一些事情时,我尝试将上述PHP代码包装在max_input_vars块中,以测试发送的HTTP方法的类型。在开发环境中,if/else返回true,但在生产环境中,if($this->input->server('REQUEST_METHOD') === 'POST')块执行并将HTTP请求记录为else(???)。服务器是否可能出于某种原因剥离请求?

0 个答案:

没有答案