vendredi 2 décembre 2016

Cross-Origin Request Blocked: issue on sending headers in Angular2 post request to laravel

I am using angular2+laravel and I am trying to post the request with headers but its giving Cross-Origin Request Blocked: error. If any body knows how to resolve it.

I did some possible solution like enable Access-Control-Allow-Origin "*" in htaccess file of laravel and created CORS Middleware but these solutions are not working. Here is my code of angular.

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map'
import configApp = require('../_configs/app.settings');
//import '../assets/frontend/custom/js/jquery.toaster.config.js';
declare function maketoast(status: string, message: string) : void;


@Injectable()
export class ForgotpasswordService {

constructor(private http: Http) { }

forgotPassword(email: string, token: string) {

    alert(token)
    var headers = new Headers();

   // headers.append('Content-Type', 'application11/json');

    let headers = new Headers();
    headers.append("Authorization", "Bearer " + token;
    let options = new RequestOptions({ headers: headers });

    // console.log(configApp.apiUrl+/test);
    return this.http.post(configApp.apiUrl+"/password/email", JSON.stringify({ email: email, apiRequest:1}), options } )
        .map((response: Response) => {

            // login successful if there's a jwt token in the response
            let user = response.json();

            console.log(user.status);
            if(user.status == false){

                maketoast(user.toaster_status, user.message);
                event.stopImmediatePropagation;
                event.preventDefault();
                event.stopPropagation();

            }


            if (user.status == true) {

                maketoast(user.toaster_status, user.message);

            }
        });

}

resetPassword(token: string, email: string, password: string,     confirmPassword: string) {


    // console.log(configApp.apiUrl+/test);
    return this.http.post(configApp.apiUrl+"/password/reset",   JSON.stringify({ token: token, email: email, password: confirmPassword, confirmPassword: password, apiRequest:1}))
        .map((response: Response) => {

            // login successful if there's a jwt token in the response
            let user = response.json();

            console.log(user.status);
            if(user.status == false){

                maketoast(user.toaster_status, user.message);
                event.stopImmediatePropagation;
                event.preventDefault();
                event.stopPropagation();

            }


            if (user.status == true) {

                maketoast(user.toaster_status, user.message);

            }
        });

}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire