dimanche 7 mai 2017

Unable to send data to laravel using ionic 2

I am working on a Hybrid application I want to sent json data to laravel php server using Ionic 2. I am continuously getting error as

XMLHttpRequest cannot load http://ift.tt/2pVjEja. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

Ionic Code: register-recipient-page.ts

this.registrationService.sendData(this.donarDetails,this.recipientDetails).subscribe(
        response => console.log(response), // success
        error => console.log(error),       // error
        () => console.log('completed')     // complete
     );

Ionic Code: registration.service.ts

 sendData(recipient,donar): Observable<Object> {

        let encoded_data = JSON.stringify({recipientDetails:recipient, donarDetails:donar});
        let headers = new Headers();
        headers.append('Content-Type', 'application/json;charset=utf-8');
        headers.append('Access-Control-Allow-Origin', '*');
        headers.append('Access-Control-Allow-Methods', 'GET, POST, PUT,DELETE, OPTIONS');
        //let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' });
        let options = new RequestOptions({ headers: headers });
         console.log(encoded_data);
        return this.http.post( 'http://ift.tt/2pVjEja',encoded_data, options).map(
            (res: Response) => res.json() || {}
        );

    }

laravel: web.php

Route::group(['middleware' => 'cors'], function(){
 Route::get('/SaveUsers', 'UserController@saveUser');
});

Cors.php

public function handle($request, Closure $next){
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT,DELETE, OPTIONS');
}

I tried to do testing using simple get on the same url without sending any data, it was working fine. Please help!!!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire