跨域请求被阻止:相同来源策略不允许读取缺少远程...... CORS标头"Access-Control-Allow-Origin" [英] Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote ...............CORS header ‘Access-Control-Allow-Origin’ missing

查看:239
本文介绍了跨域请求被阻止:相同来源策略不允许读取缺少远程...... CORS标头"Access-Control-Allow-Origin"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://localhost:3000/edata

[{"_id":"598705ac8f79380367e0a7f2","name":"prasad","age":"28","gender":"male","phone":8790440944},{"_id":"598733508f79380367e0a7f8","name":"ravi","age":"27","gender":"male","phone":"9912881777"}

我希望在运行客户端应用程序时获取此数据,即 http://localhost:4200

i want this data to be get when i run my client application i.e http://localhost:4200

app.module.ts

app.module.ts

 import { BrowserModule } from '@angular/platform-browser';
 import { NgModule } from '@angular/core';
 import {HttpModule} from "@angular/http";
 import { AppComponent } from './app.component';
 import {TasksComponent} from "../tasks/tasks.component";
 import {TaskServices} from "./services/task.services";

 @NgModule({
declarations: [AppComponent, TasksComponent],
imports: [BrowserModule,HttpModule],
providers: [TaskServices],
bootstrap: [AppComponent,TasksComponent]
})
 export class AppModule { }

tasks.component.ts

tasks.component.ts

 import {Component, enableProdMode} from '@angular/core';
 import {TaskServices} from "../app/services/task.services";
 enableProdMode();
 @Component({
 selector: 'tasks',
 templateUrl: './tasks.component.html',
 styleUrls: ['./tasks.component.css']
 })
 export class TasksComponent {
 constructor(private taskServices:TaskServices){
 this.taskServices.getTasks()
       .subscribe(tasks =>{
        console.log(tasks);
      });
  }
  title = 'app';
 }

task.services.ts

task.services.ts

 import {Injectable} from '@angular/core';
 import {Http, Headers} from "@angular/http";
  import 'rxjs/add/operator/map';

 @Injectable()
 export class TaskServices{
  constructor(private http:Http){
 console.log('Task service initialized....');
  }
    getTasks(){
   return this.http.get('http://localhost:3000/edata')
    .map(res => res.json());
  }

当我运行应用程序时,在控制台中我收到错误跨域请求被阻止的消息:同源策略"不允许读取

when i run application ,in the console i am getting the error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3000/edata. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

如何解决该错误以及如何从其他主机获取数据...请帮助我.

how to Fix the error.and how to get data from the other host...plz help me.

推荐答案

如果使用 Node-server ,请将其添加到server.js

If using Node-server add this in your server.js

app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

并在您的http方法中添加听到者

and Add Hearders in your http method

return this.http.get(
  'http://localhost:3000/edata',{
    headers: {'Content-Type', undefined} /** Use Content-type as your requirement undifined OR application/json**/
  }).map(res => res.json())

这篇关于跨域请求被阻止:相同来源策略不允许读取缺少远程...... CORS标头"Access-Control-Allow-Origin"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆