简体   繁体   English

无法从extjs ajax接收post参数

[英]Can't receive post parameters from extjs ajax

I'm trying to persist grid updates to database, so I write in my controller: 我正在尝试将网格更新持久化到数据库,所以我在我的控制器中写道:

    //...
    editUser: function(button, eventObj) {   
    var view = Ext.widget('useredit');
    view.down('form').loadRecord(this.getSelected());
},

getSelected: function() {
    var grid = this.getUserList();
    var selectedRecord = grid.getSelectionModel().getSelection()[0];
    return selectedRecord;
},
updateUser: function(button) {
    var win = button.up('window'),
            form = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();
    record.set(values);
    form.updateRecord();
    win.close();
    this.getUsersStore().sync();
}
});

And this is my store code: 这是我的商店代码:

Ext.define('MyApp.store.Users', {
extend: 'Ext.data.Store',
model: 'MyApp.model.User',
autoLoad: true,
proxy: {
    type: 'ajax',
    actionMethods:
            {create: 'POST', read: 'GET', update: 'POST', destroy: 'POST'},
    api: {
        create: 'data/createUser.php',
        read: 'data/getUsers.php',
        update: 'data/updateUser.php',
        destroy: 'data/deleteUser.php'
    },
    reader: {
        type: 'json',
        root: 'users',
        successProperty: 'success'
    }
}

}); });

When I hit "save" button firebug tells me that POST request has been sent to my php file, but when I print_r($_POST) in php it shows me an empty array. 当我点击“保存”按钮时,firebug告诉我POST请求已经发送到我的php文件,但是当我在php中打印__($ _ POST)时它会显示一个空数组。 How can this be? 怎么会这样? 在此输入图像描述

By default, the proxy's writer will send the JSON string as raw POST data to the server. 默认情况下,代理的writer会将JSON字符串作为原始POST数据发送到服务器。 PHP's $_POST superglobal takes variables/parameters from the raw POST data, if possible, eg if it contains url-encoded data: 如果可能的话,PHP的$_POST超全局从原始POST数据中获取变量/参数,例如,如果它包含url编码的数据:

var1=a&var2=b&var3=c

However, since your POST data is just a JSON string, there are no key=value pairs to extract from, which is why your $_POST array is empty. 但是,由于您的POST数据只是一个JSON字符串,因此没有要从中提取的key = value对,这就是您的$_POST数组为空的原因。

To read the raw post data, you can use PHP's input stream php://input : 要读取原始发布数据,可以使用PHP的输入流php://input

$json = file_get_contents('php://input');
$data = json_decode($json);
var_dump($data);

Alternatively, in ExtJS you can also tell the writer to pass the JSON string as a HTTP parameter to the request using the encode and root configurations: 或者,在ExtJS中,您还可以告诉编写器使用encoderoot配置将JSON字符串作为HTTP参数传递给请求:

proxy: {
    // ...
    writer: {
        type: 'json',
        encode: true,
        root: 'users'
    }
}

Which will result in the writer generating the POST data like this: 这将导致编写器生成POST数据,如下所示:

users={"var1":"a","var2":"b","var3":"c"}

and therefore allows you to use the $_POST variable in PHP: 因此允许您在PHP中使用$_POST变量:

$data = json_decode($_POST['users']);
var_dump($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM