No' Access-Control-Allow-Origin'标头出现在请求的资源上。起源' http:// localhost:4200'因此不允许访问

时间:2017-08-16 18:49:17

标签: php json cors angular2-services

在我的角度项目中,我正在调用rest api,但是当我打电话给我时,我发现了错误。我试图添加标题,但我无法解决。所有这些都未能解决。

  

如何在角度2中添加cors(请提供完整的导入和代码)   我是否必须在服务器

中的restapi页面中启用代码

在localhostL中查看我的angular2项目:4200

<defs>
    <path id="eventPath" d="M49.2,200c20.5-16.4,92.9-70,200-
    70c26.3,0,116.3,3.2,200,70"/>
    <path id="namePath" d="M49.2,380c20.5,16.4,92.9,70,200,70c26.3,0,116.3-
    3.2,200-70 "/>
</defs>

<text id="event" text-anchor="middle" class="st0 st1">
    <textPath id="ep" xlink:href="#eventPath" startOffset="50%">
        {{event}}
    </textPath>
</text>
<script type="text/javascript">
    var textPath = document.getElementById("ep");
    var tpLength = textPath.getBBox();
    var path = document.getElementById("event");
    var pLength = path.getBBox();
    var fontSize = 65;
    while (tpLength.width > pLength.width) {
        fontSize -= 0.1;
        textPath.setAttribute("font-size", fontSize);
    }
</script>

和我的php页面

import {Injectable} from '@angular/core';
import 'rxjs/Rx';
import { Http , Response, Headers, RequestOptions } from'@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import 'rxjs/add/observable/throw';

@Injectable()
 export class LoginService {
 constructor(private _http:Http){}

  getCurrentTime(){
 var header = new Headers();
 header.append('contentType','application/json; charset=utf-8');
 header.append('Accept','application/json');
 header.append('Access-Control-Allow-Origin', '*');
var option = new RequestOptions();
 option.headers = header;
 return 
this._http.get('http://is.com/equityjs.php',option)
.map(res=>res.json());

 }

}

1 个答案:

答案 0 :(得分:1)

您必须在服务器端允许原点,请在您的代码中尝试:

 <?php
 header("Access-Control-Allow-Origin: *");