Fix Ajax endpoint documentation - remove incorrect success wrapper pattern
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -963,47 +963,47 @@ Details: `php artisan rsx:man file_upload`
|
|||||||
|
|
||||||
## AJAX ENDPOINTS
|
## AJAX ENDPOINTS
|
||||||
|
|
||||||
|
**Return data directly - framework auto-wraps as `{success: true, data: ...}`**
|
||||||
|
|
||||||
```php
|
```php
|
||||||
#[Ajax_Endpoint]
|
#[Ajax_Endpoint]
|
||||||
public static function get_data(Request $request, array $params = [])
|
public static function fetch_client(Request $request, array $params = []) {
|
||||||
{
|
$client = Client_Model::find($params['id']);
|
||||||
return ['success' => true, 'data' => ...];
|
if (!$client) throw new \Exception('Not found');
|
||||||
|
return $client; // ✅ Return data directly
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript
|
**❌ NEVER**: `return ['success' => true, 'data' => $client]` - framework adds this
|
||||||
const result = await Demo_Controller.get_data({user_id: 123});
|
|
||||||
|
**Special returns** (bypass auto-wrap):
|
||||||
|
- `['errors' => [...]]` - Form validation errors
|
||||||
|
- `['redirect' => '/path']` - Client-side navigation
|
||||||
|
- `['error' => 'msg']` - Operation failure
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Form validation
|
||||||
|
if (empty($params['name'])) {
|
||||||
|
return ['errors' => ['name' => 'Required']];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Success with redirect
|
||||||
|
Flash_Alert::success('Saved');
|
||||||
|
return ['redirect' => Rsx::Route('View_Action', $id)];
|
||||||
|
|
||||||
|
// Permission error
|
||||||
|
if (!can_delete()) {
|
||||||
|
return ['error' => 'Permission denied'];
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### Flash Alerts in Ajax Endpoints
|
### Flash Alerts in Ajax Endpoints
|
||||||
|
|
||||||
**Server-side: Use success alerts ONLY when providing a redirect.**
|
Use server-side success alerts ONLY with redirects (no on-screen element to show message).
|
||||||
|
|
||||||
When the Ajax response includes a redirect, there's no on-screen element from the previous page to display the success message. Flash alerts persist across navigation to show the result.
|
**Client-side**: Use `Flash_Alert.error()`, `Flash_Alert.warning()` on case-by-case basis.
|
||||||
|
|
||||||
```php
|
|
||||||
#[Ajax_Endpoint]
|
|
||||||
public static function save(Request $request, array $params = []): array {
|
|
||||||
// Validation errors - return for client to handle
|
|
||||||
if (empty($params['name'])) {
|
|
||||||
return ['success' => false, 'errors' => ['name' => 'Required']];
|
|
||||||
}
|
|
||||||
|
|
||||||
$client->save();
|
|
||||||
|
|
||||||
// Success with redirect - use flash alert
|
|
||||||
Flash_Alert::success('Client saved successfully');
|
|
||||||
|
|
||||||
return [
|
|
||||||
'client_id' => $client->id,
|
|
||||||
'redirect' => Rsx::Route('Clients_View_Action', $client->id),
|
|
||||||
];
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Server pattern:** Success alerts with redirect only. Client handles errors by updating on-screen UI.
|
|
||||||
|
|
||||||
**Client-side:** Flash alerts are a UI choice. Use `Flash_Alert.error()`, `Flash_Alert.warning()`, etc. on case-by-case basis (e.g., "Failed to add item to cart - item not available").
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user