const apiUrl = 'https://api.chucknorris.io/jokes/random';
fetch(apiUrl)
.then(response => response.json())
.then(data => console.log('Chuck Norris Joke:', data.value))
.catch(error => console.error('Error:', error));Fetching Chuck Norris Jokes from API in JavaScript
Related Posts
More content you might like
Code
javascript
Dynamic and Responsive DataTable with Server-Side Processing and Custom Styling
initComplete: Adds custom classes to dropdowns and inputs in the DataTables UI for consistent styling.
drawCallback: Applies custom classes to various table elements (e.g., rows, headers, cells) after each table draw to enhance the appearance.
Oct 24, 2024
Read More Tutorial
php
Handling HTTP Requests and Raw Responses in Laravel
When working with OAuth or JWT-based APIs, you may need to pass a Bearer Token for authentication.
use Illuminate\Support\Facades\Http;
$response = Http::withToken('your-bearer-token')->post('https://api.example.com/endpoint', [
'key1' => 'value1',
'key2' => 'value2',
]);
dd($response->body());Oct 24, 2024
Read More Article
javascript
20 Useful Node.js tips to improve your Node.js development skills:
No preview available for this content.
Oct 24, 2024
Read More Tutorial
javascript
AJAX with JavaScript: A Practical Guide
- The
fetch()method returns a Promise that resolves to the Response object representing the entire HTTP response. - We check if the response is successful and then parse it as JSON.
- Any errors during the request are caught and logged.
In many real-world applications, you'll need to send data to the server using POST requests. Here's how to send form data using the Fetch API.
Sep 18, 2024
Read MoreDiscussion 0
Please sign in to join the discussion.
No comments yet. Be the first to share your thoughts!