2016-11-07 83 views
2

我有以下问题:Angular 2 HTTP服务从https切换到http模式

在我从PHP脚本中检索数据的角度服务中,浏览器或角度从https切换到http。我的站点通过HTTPS加载HTS,因此ajax请求被阻止为混合内容。

我已经为我的AJAX服务下面的代码:

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

@Injectable() 
export class AjaxService { 
    private apiUrl: string = "https://example.com/myapi/myscript"; 

    /** 
    * @param {Http} http HTTPService 
    */ 
    constructor(private http: Http) { } 

    /** 
    * @param {string} piece 
    */ 
    public getAllComponents(piece: string) { 
     console.debug("Get Components for Query: " + piece); 
     return this.http.get(this.apiUrl + "?compontent=" + piece).map((res: Response) => res.json()); 
    } 

    [...] 

} 

当我打电话从https://example.com/Angular2Application主页我的网页,并触发一个请求,我的浏览器告诉我Ajax请求被阻止混合内容,并告诉我他试图连接到http://example.com/myapi/myscript

回答

0

只提供一个网址,没有该计划。例如:

private apiUrl: string = "//example.com/myapi/myscript"; 

您的浏览器将匹配您当前页面的方案,以避免混合内容问题。

+0

我已经尝试这样做了。再试一次,是的...它仍然需要http方案;( –

+0

可能有某种网络服务器重定向呢? – vidalsasoon

+0

不,这真的很奇怪... –

0

现在它的工作。没有计划为什么,但没问题。

关闭