Skip to main content

How to call API in angular & with an example of PHP API


How to call API in angular & with an example of PHP API.

Software required:-


  • Angular CLI -> npm install -g @angular/cli

To understand how to call API in angular follow the below steps:-



Step:1 Project Setup


1. Create a project in angular -> ng new project name
2. Install npm -> npm i

3. Import HttpClientModule in app.module.ts -> import { HttpClientModule } from '@angular/common/http';

4. Add provider providers: [WebserviceService],

example -> ng new crudInAngularPHP



Step:2 API Service


1. Create a service with name webservice -> ng g s services/webservice
2. import modules import { HttpClient, HttpHeaders, HttpRequest, HttpEvent } from '@angular/common/http';
3 .    Call constructor constructor(private http: HttpClient)

4. Add a common function to get a response of API with name postRequest:-
    

  
     postRequest(url: string, data: any): any {
    let headers = new HttpHeaders({
'Content-Type': 'application/json',
'Accept': 'application/json',
})
let payload = data;
let endPoint =“api base url” + url;
return this.http.post(endPoint, payload, { headers });

    }


Step:3 Components and API Calling


1. Create a component with name home -> ng g c home
2. Design .HTML file
3. Call web service in .ts file
        1. Import import { WebserviceService } from '../services/web.service';

        2. Call consturctor constructor( private webapi: WebserviceService ) { }

4. Create a function to get Channel data :-


    getRecord() {
let requestData:any={}
this.webapi.postRequest("API URL", requestData).
subscribe(
data => {
// write logic on data
},
error => {
}
);
}




PHP API CODE EXAMPLE:-



<?php

    header("Access-Control-Allow-Origin: http://localhost:4545"); 

    header("Content-Type: application/json; charset=UTF-8");  

  

    header("Access-Control-Allow-Methods: POST, DELETE, OPTIONS");  

  

    header("Access-Control-Max-Age: 3600");  

 

    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");  


    $response['name'] = "Mukund Programming Tutorials";


    $response['Courses'] = [array("name"=>"Angular","name"=>"JavaScript")];


    $response['status'] = true;


    $json_response = json_encode($response);


    echo $json_response;


 

 ?>




Comments

Popular posts from this blog

What are the data types and variables in JavaScript? and example of sum operation on hindi variables.

Datatypes in JavaScript There are majorly two types of languages. First, one is  Statically typed language  where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types. Example: C, C++, Java.  // Java(Statically typed) int x = 5   // variable x is of type int and it will not store any other type. string y = 'abc' // type string and will only accept string values Other,  Dynamically typed languages : These languages can receive different data types over time. For example- Ruby, Python, JavaScript etc. // Javascript(Dynamically typed) var x = 5; // can store an integer var name = 'string' ; // can also store a string. JavaScript is a dynamically typed (also called loosely typed) scripting language. That is, in JavaScript variables can receive different data types over time. Datatypes are basically typed data that can be used and manipulated in a pr...

Traffic light program using javascript | An automated traffic signal project in javascript

  TRAFFIC SIGNAL PROJECT WITH AUTO COUNT DOWN

Conditional statement in javascript| Alternative of Conditional Statements

Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run. ... “ If” statements : where if a condition is true it is used to specify execution for a block of code. “Else” statements: where if the same condition is false it specifies the execution for a block of code. How to use Conditional Statements Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action. Different Types of Conditional Statements There are mainly three types of conditional statements in JavaScript. If statement If…Else statement If…Else If…Else statement 1) If statement Syntax: if (condition) { lines of code to be executed if condition is true } You can use If statement if you want to check only a specific condition. Try this yourself: <html> <head> <title>IF Statments!!!</title> <script typ...