AJAX
SØAD provides seamless AJAX support through JSON responses and form handling. This recipe demonstrates how to create interactive web applications using different JavaScript approaches - from vanilla JavaScript to modern libraries like jQuery and Axios.
Basic AJAX with Vanilla JavaScript
| user_search.html | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |
CREATE TABLE user (
id int NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
email varchar(150) NOT NULL,
department varchar(50),
phone varchar(20),
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY email (email)
);
INSERT INTO user (name, email, department, phone) VALUES
('Alice Johnson', '[email protected]', 'Engineering', '+1-555-0101'),
('Bob Smith', '[email protected]', 'Marketing', '+1-555-0102'),
('Carol Davis', '[email protected]', 'Engineering', '+1-555-0103'),
('David Wilson', '[email protected]', 'Sales', '+1-555-0104'),
('Eva Brown', '[email protected]', 'HR', '+1-555-0105'),
('Frank Miller', '[email protected]', 'Engineering', '+1-555-0106'),
('Grace Lee', '[email protected]', 'Finance', '+1-555-0107'),
('Henry Taylor', '[email protected]', 'Marketing', '+1-555-0108'),
('Iris Chen', '[email protected]', 'Engineering', '+1-555-0109'),
('Jack Anderson', '[email protected]', 'Sales', '+1-555-0110');
How It Works
- Search as you type: Real-time user search with vanilla JavaScript
- Modal details: Click any user card to view detailed information
- Error handling: Proper error messages and loading states
- Responsive design: Bootstrap-powered responsive layout
AJAX Form Submission with jQuery
| contact_form.html | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
CREATE TABLE contact (
id int NOT NULL AUTO_INCREMENT,
name varchar(100) NOT NULL,
email varchar(150) NOT NULL,
subject varchar(200) NOT NULL,
message text NOT NULL,
status enum('new','read','replied') DEFAULT 'new',
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_email (email),
KEY idx_status (status)
);
Key Features
- Real-time validation: Email validation as you type
- Form serialization: jQuery handles form data automatically
- Loading states: Visual feedback during submission
- Error handling: Field-specific error messages
Modern AJAX with Fetch API
| task_manager.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
| task_manager.html | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | |
CREATE TABLE task (
id int NOT NULL AUTO_INCREMENT,
title varchar(200) NOT NULL,
description text,
status enum('pending','in_progress','completed') DEFAULT 'pending',
priority enum('low','medium','high') DEFAULT 'medium',
due_date date,
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_status (status),
KEY idx_priority (priority)
);
INSERT INTO task (title, description, priority, due_date) VALUES
('Complete project proposal', 'Write and submit the Q1 project proposal document', 'high', '2024-02-15'),
('Code review session', 'Review pull requests from team members', 'medium', '2024-02-10'),
('Update documentation', 'Update API documentation with new endpoints', 'low', '2024-02-20'),
('Team meeting preparation', 'Prepare slides for weekly team meeting', 'medium', '2024-02-08'),
('Bug fixes', 'Fix reported bugs in the user dashboard', 'high', '2024-02-12');
Modern Features
- ES6 Classes: Object-oriented JavaScript architecture
- Fetch API: Modern promise-based HTTP requests
- Async/Await: Clean asynchronous code handling
- CRUD Operations: Complete task management functionality
Do you know?
- Fetch API is modern and built into all browsers, no library needed
- FormData automatically handles form serialization and file uploads
- Async/await makes code more readable than traditional callbacks
- Error boundaries ensure graceful degradation when requests fail