📚 احصل على أحدث المقالات فور نشرها — مع وصول كامل لمكتبتنا من الكتب المجانية — افتح المكتبة

تنبيه

الرجاء تحديد نص من المقال أولاً.

Ahmed Bouchefra

🖨️

جاري تحضير PDF...

الرئيسية
Ahmed Bouchefra

أحمد بوشفرة

Software Engineer & Tech Author

كتابي على Packt
كتابي على Packt
ابدأ هنا
ابدأ هنا
المكتبة
المكتبة
أكاديمية بايثون
أكاديمية بايثون
تطبيق اختبارات البرمجة
تطبيق اختبارات البرمجة
دورات يوديمي
دورات يوديمي
المسارات
المسارات
الملخصات
الملخصات
الأدوات
الأدوات
اشترك
اشترك
كتب فريق 10xdevblog
كتب فريق 10xdevblog
الكاتب: أحمد بوشفرة

Angular 10 Example: Import HttpClientModule and Send Http Ajax Requests to JSON REST API Servers

اضغط على زر PDF لتحميل المقال كملف للقراءة لاحقاً

ملاحظة: هذا المقال بقلم أحمد بوشفرة. الآراء الواردة تعبر عن الكاتب.

أحمد بوشفرة: مبرمج ومؤلف تقني، أساعد المطورين على بناء تطبيقات ويب حديثة وسريعة.

يمكنك التواصل مع الكاتب لطلب خدمات برمجية عبر:

يمكنك أيضاً نشر مقالك هنا والترويج لخدماتك أمام جمهور من المبرمجين. تواصل معنا

لخّص هذا المقال مع ChatGPT

انسخ الأمر وألصقه في ChatGPT:

لخص لي هذا المقال في نقاط رئيسية: https://www.ahmedbouchefra.com/angular-httpclient-module/ فتح ChatGPT ↗

HttpClientModule configures the dependency injector for HttpClient with supporting services for XSRF.

In this example, we’ll see how to import HttpclientModule in Angular and use HttpClient to send an http Ajax GET request to JSON REST API servers.

What is HttpClient and how it Relates to Ajax?

HttpClient is a service for handling HTTP requests which is built on top of XMLHttpRequest which the legacy API for doing Ajax.

What About Ajax?

Ajax stands for Asynchronous JavaScript and XML and is a technique for requesting data from the server without doing a full page refresh, and using the XML result to re-render the related part of the page.

In the modern web, Ajax refers to any asynchronous request sent to a server from client-side JavaScript. Typically the response is JSON, or HTML fragments instead of XML.

How to Use HttpClient?

It can be injected in other services and components. It exports methods for making HTTP Ajax requests such as get(), post(), put(), and delete() and return http responses with various types such as json, text and blob.

HttpClient provides many benefits such as testability, typed request and response objects, request and response interception, Observable APIs, and streamlined error handling.

The methods of HttpClient are based on RxJS observables, so we need to be aware of these points when using them:

  1. You need to subscribe to the observable returned from the method to actually send the http request to the server,
  2. If you subscribe multiple times to the returned observable, multiple HTTP requests will be sent to the server,
  3. The returned observable is a single-value stream which means it will emit only one value and complete.

Step 1 - Creating an Angular 10 Project

If ou are new to these quick how-to posts, you need to install Angular CLI and scaffold a new project

Step 2 - Importing HttpClientModule?

Before you can use HttpClient in your Angular 10 application, you need to import HttpClientModule.

Open the src/app/app.module.ts file and import HttpClientModule from @angular/common/http:

import { NgModule }         from '@angular/core';  
import { BrowserModule }    from '@angular/platform-browser';  
import { HttpClientModule } from '@angular/common/http';

Next, you need to add it HttpClientModule as follows:

@NgModule({  
imports: [  
BrowserModule,  
// import HttpClientModule after BrowserModule.  
HttpClientModule,  
],  
declarations: [  
AppComponent,  
],  
bootstrap: [ AppComponent ]  
})  
export class AppModule {} 

Step 3 - Using Angular 10 HttpClient to Send Ajax GET Requests

After you imported HttpClientModule, you can send http requests using the HttpClient service which you can inject in any service or component.

Open the src/app/app.component.ts file and start by importing HttpClient as follows:

import { HttpClient } from '@angular/common/http';

Next, inject HttpClient via the constructor of the component as follows:

  constructor(public httpClient: HttpClient){}

Next, define the following example method:

    sendGetRequest(){
        this.httpClient.get('https://jsonplaceholder.typicode.com/users').subscribe((res)=>{
            console.log(res);
        });
    }

This method sends a GET request to the server endpoint and subscribe to the returned observable.

Next, open the src/app/app.component.ts file and add a button to call the sendGetRequest() method as follows:

    <button (click)="sendGetRequest()">GET Users</button>

References

Conclusion

In this quick how-to post, we have seen how to import HttpClientModule in Angular 10 and used the HttpClient service to send an example http Ajax GET request to a REST API server server.

تعلم البرمجة، الذكاء الاصطناعي والأمن السيبراني

اكتشف عجائب عالم التقنية مع أحمد بوشفرة. انضم إلينا اليوم للوصول إلى أحدث الدروس والمقالات.

اقرأ المزيد عن الكاتب

انضم إلى مجتمعاتنا

تابعنا على منصات التواصل الاجتماعي للحصول على أحدث التحديثات والمقالات اليومية.

فيسبوك قناة مجتمع تيليغرام
المزيد من الحسابات...

مكتبة الكتب المجانية

حمّل كتب وأدلة PDF مجانية في البرمجة وتطوير الويب والذكاء الاصطناعي.

تصفح المكتبة

احصل على المحتوى الجديد فور نشره ⚡

اختر الطريقة الأنسب لك لمتابعتنا والحصول على التحديثات مجاناً.
(اضغط على رابط التفعيل الذي سيصلك لفتح المحتوى)

عرض كل بدائل الاشتراك

احصل على موارد مجانية! 📚

اشترك في القائمة البريدية واحصل على كتب ومصادر تعليمية مجانية

شارك المقال