2

I have the following problem:

In my angular service for retrieving data from an PHP script the browser or angular switches from https to http. My site is loaded over HTTPS with HTS so the ajax request is blocked as mixed content.

I have the following code for my ajax service:

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());
    }

    [...]

}

When i call my page from the main page https://example.com/Angular2Application and fire an request my browser tell me the ajax request was blocked for mixed content and tells me he tried to connect to http://example.com/myapi/myscript

2 Answers 2

1

Just provide a url without the scheme. Ex:

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

Your browser will match the scheme of your current page so you can avoid mixed content problems.

Sign up to request clarification or add additional context in comments.

2 Comments

I have already tried this and it. Gave it a try again and yeah ... it still takes the http scheme ;(
there's probably some sort of web server redirection then?
0

Now it works. No plan why, but ok.

Closed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.