Posts

Showing posts from June, 2023

Error while calling API in .net core use responseType in get method

  this . http . get ( this . baseURL + 'api/Values' , { responseType : 'text' }). subscribe ( payload =>     this .ArrayList = payload );

Access to XMLHttpRequest at 'https://localhost:44363/api/Values' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Access to XMLHttpRequest at 'https://localhost:44363/api/Values' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Error in .net core API of CORS add first red color line and with origins is your link of angular. and 2nd is  Configure method with red color in startup class both in startup.cs.  public void ConfigureServices(IServiceCollection services)         {             //services.AddCors(o => o.AddPolicy("MyPolicy", builder =>             //{             //    builder.AllowAnyOrigin()             //           .AllowAnyMethod()             //           .AllowAnyHeader()             //           ;             //}));             services.AddCors(options => options.AddDefaultPolicy(builder =>             {                 builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader();             }));             s

How to Connect angular with .net core API and get response in Array

 Here is the example of ts file where service is calling and hold in Array list. import { Component } from '@angular/core' ; import { HttpClient, HttpHeaders } from '@angular/common/http' ; import { Observable, map } from 'rxjs' ; @ Component ({   selector: 'app-login' ,   templateUrl: './login.component.html' ,   styleUrls: [ './login.component.css' ] }) export class LoginComponent {   baseURL : string = "https://localhost:44363/" ;   Model : any ;   ArrayList : any ;     constructor ( private http : HttpClient ) {   }   onSave (){     //alert("adasd");     const headers = { 'Access-Control-Allow-Origin' : '*' , 'Access-Control-Allow-Methods' : "POST" , 'Access-Control-Allow-Headers' : "accept, content-type" }       const body = "" ;     console. log (body) //  this.http.get(this.baseURL + 'api/Values', {responseType: 'tex