Sencha ExtJS REST服务中使用POST方法保存Model的问题排查
Hey there, let's fix this issue where your Sencha Model is sending a GET request instead of POST when saving. Here's what you need to check and adjust:
1. 检查并修正Model的Proxy配置
The most common culprit is an incorrect proxy setup. Sencha's Model uses different HTTP methods based on the action (create/read/update/destroy), but you can explicitly define which method maps to which action via actionMethods.
Here's a corrected Model configuration example:
Ext.define('YourApp.model.YourModel', { extend: 'Ext.data.Model', fields: [ { name: 'id', type: 'int' }, { name: 'title', type: 'string' }, // Add your other fields here ], proxy: { type: 'ajax', // Or 'rest' if you're using a RESTful API url: '/your-server/save-endpoint', // Your backend URL // Explicitly set HTTP methods for each action actionMethods: { create: 'POST', // This ensures new records use POST read: 'GET', update: 'PUT', // Change to 'POST' if your backend expects POST for updates destroy: 'DELETE' }, // Make sure you have a writer configured to serialize data writer: { type: 'json', writeAllFields: true, // Sends all fields, not just changed ones encode: true // Adjust based on your backend's data format needs }, reader: { type: 'json', rootProperty: 'data' // Match your backend's response structure } } });
2. 确认你保存的是新记录
Sencha uses isNew() to determine if a record is new (needs POST) or existing (needs PUT by default). If your record has an id value set (even a default like 0), Sencha might treat it as an existing record and use PUT instead of POST.
Check this with a quick console log before saving:
var newModel = Ext.create('YourApp.model.YourModel', { title: 'My New Record' }); console.log(newModel.isNew()); // Should return true for new records
If it returns false, double-check that your id field isn't being auto-set to a non-null value.
3. 检查保存调用的代码
Your save call should be straightforward, but make sure you're not accidentally triggering a read/load action instead of save:
newModel.save({ success: function(record, operation) { console.log('Record saved successfully!'); }, failure: function(record, operation) { console.error('Save failed:', operation.getError()); } });
4. 如果你使用RestProxy
If you're using a rest type proxy, the default mappings are RESTful (create=POST, update=PUT), but you can override them the same way with actionMethods if your backend doesn't follow REST conventions.
Debug Tip
Use your browser's DevTools (Network tab) to inspect the request:
- Check the HTTP method being sent
- Look at the request payload to ensure data is being sent correctly
- Verify the request URL matches what you configured in the proxy
内容的提问来源于stack exchange,提问作者Hadi Nahavandi

