Fix: Corregir formato AJAX WordPress en Admin Panel
Causa raíz del error 400: El código estaba usando formato JSON cuando WordPress AJAX requiere application/x-www-form-urlencoded.
## Problema
**Error:** POST admin-ajax.php 400 (Bad Request)
**Causas:**
1. JavaScript enviaba datos como JSON (`Content-Type: application/json`)
2. PHP usaba `json_decode(file_get_contents('php://input'))`
3. WordPress AJAX espera `$_POST` con `action` y `nonce`
## Solución
### JavaScript (admin-app.js)
```javascript
// ANTES (❌ Incorrecto)
data: JSON.stringify({
action: 'apus_save_settings',
nonce: apusAdminData.nonce,
...formData
})
// AHORA (✅ Correcto)
const postData = new URLSearchParams();
postData.append('action', 'apus_save_settings');
postData.append('nonce', apusAdminData.nonce);
postData.append('components', JSON.stringify(formData.components));
```
### PHP (class-settings-manager.php)
```php
// ANTES (❌ Incorrecto)
$data = json_decode(file_get_contents('php://input'), true);
// AHORA (✅ Correcto)
$components = json_decode(stripslashes($_POST['components']), true);
```
### Cambios Aplicados
**admin-app.js:**
- Usar `URLSearchParams` en lugar de `JSON.stringify`
- Header `application/x-www-form-urlencoded`
- Enviar `components` como JSON string en parámetro POST
**class-settings-manager.php:**
- Verificar nonce con `wp_verify_nonce($_POST['nonce'])`
- Parsear `$_POST['components']` como JSON
- Mensajes de error más descriptivos
## WordPress AJAX Requirements
1. ✅ `action` en $_POST
2. ✅ `nonce` en $_POST
3. ✅ Content-Type: application/x-www-form-urlencoded
4. ✅ Usar $_POST, no php://input
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -119,17 +119,21 @@ const AdminPanel = {
|
|||||||
try {
|
try {
|
||||||
const formData = this.collectFormData();
|
const formData = this.collectFormData();
|
||||||
|
|
||||||
|
// Crear FormData para WordPress AJAX
|
||||||
|
const postData = new URLSearchParams();
|
||||||
|
postData.append('action', 'apus_save_settings');
|
||||||
|
postData.append('nonce', apusAdminData.nonce);
|
||||||
|
|
||||||
|
// Agregar components como JSON string
|
||||||
|
postData.append('components', JSON.stringify(formData.components));
|
||||||
|
|
||||||
const response = await axios({
|
const response = await axios({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: apusAdminData.ajaxUrl,
|
url: apusAdminData.ajaxUrl,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/x-www-form-urlencoded'
|
||||||
},
|
},
|
||||||
data: JSON.stringify({
|
data: postData
|
||||||
action: 'apus_save_settings',
|
|
||||||
nonce: apusAdminData.nonce,
|
|
||||||
...formData
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.success) {
|
if (response.data.success) {
|
||||||
|
|||||||
@@ -144,7 +144,10 @@ class APUS_Settings_Manager {
|
|||||||
* AJAX: Obtener configuraciones
|
* AJAX: Obtener configuraciones
|
||||||
*/
|
*/
|
||||||
public function ajax_get_settings() {
|
public function ajax_get_settings() {
|
||||||
check_ajax_referer('apus_admin_nonce', 'nonce');
|
// Verificar nonce
|
||||||
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'apus_admin_nonce')) {
|
||||||
|
wp_send_json_error('Nonce inválido');
|
||||||
|
}
|
||||||
|
|
||||||
if (!current_user_can('manage_options')) {
|
if (!current_user_can('manage_options')) {
|
||||||
wp_send_json_error('Permisos insuficientes');
|
wp_send_json_error('Permisos insuficientes');
|
||||||
@@ -158,18 +161,30 @@ class APUS_Settings_Manager {
|
|||||||
* AJAX: Guardar configuraciones
|
* AJAX: Guardar configuraciones
|
||||||
*/
|
*/
|
||||||
public function ajax_save_settings() {
|
public function ajax_save_settings() {
|
||||||
check_ajax_referer('apus_admin_nonce', 'nonce');
|
// Verificar nonce
|
||||||
|
if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'apus_admin_nonce')) {
|
||||||
|
wp_send_json_error('Nonce inválido');
|
||||||
|
}
|
||||||
|
|
||||||
if (!current_user_can('manage_options')) {
|
if (!current_user_can('manage_options')) {
|
||||||
wp_send_json_error('Permisos insuficientes');
|
wp_send_json_error('Permisos insuficientes');
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
// Los datos vienen como JSON string en $_POST['components']
|
||||||
|
if (!isset($_POST['components'])) {
|
||||||
if (!$data) {
|
wp_send_json_error('Datos inválidos - falta components');
|
||||||
wp_send_json_error('Datos inválidos');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$components = json_decode(stripslashes($_POST['components']), true);
|
||||||
|
|
||||||
|
if (!is_array($components)) {
|
||||||
|
wp_send_json_error('Datos inválidos - components no es un array válido');
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = array(
|
||||||
|
'components' => $components
|
||||||
|
);
|
||||||
|
|
||||||
$result = $this->save_settings($data);
|
$result = $this->save_settings($data);
|
||||||
|
|
||||||
if ($result['success']) {
|
if ($result['success']) {
|
||||||
|
|||||||
Reference in New Issue
Block a user