I use Cors in laravel, my cors.php is
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE,
OPTIONS');
header('Content-Type:application/json; charset=utf-8');
}
}
Page root is in laravel web.php;
Route::group(['middleware' => 'cors'], function()
{
Route::get('/news_json', 'PagesController@news_json');
Route::post('/post_request', 'PagesController@post_request');
});
Now, in my ionic app; When I post data this get error. Error.status is '419'. I dont use csrf_token in ionic app. I have a function in ionic;
getToken() {
let token = document.querySelector('meta[property="csrf-token"]')
['content'];
return token;
}
I use meta property in ionic3/src/index.html , this meta propery is;
<meta property="csrf-token" name="csrf-token" content="{{
csrf_token() }}">
My submit function in ionic ;
var encrypt = new Encrypt.JSEncrypt();
encrypt.setPublicKey(pubkey);
var link ='http://172.16.1.19/post_request';
var myData = JSON.stringify({name: this.data.name, telephone: this.data.telephone, email: this.data.email, ask: this.data.ask});
var encrypted = encrypt.encrypt(myData);
var post = {form: encrypted, type: 'baskan'};
var options = new RequestOptions({headers: new Headers({
'Content-Type' : 'application/x-www-form-urlencoded',
'X-CSRF-TOKEN': this.getToken()
})
});
this.http.post(link, post, options).then((data) => {
this.toast.create({
message: `İsteğiniz başarıyla gönderildi!.`,
duration: 3000
}).present();
this.data.name="";
this.data.telephone="";
this.data.email="";
this.data.ask="";
}, error =>{
this.data.ask = error.status;
});
I post to laravel other script but when I post data with ionic ı get a error, this error.status is '419' . Can you help me please ?
You must add your post location in App/Middleware/VerifyCsrfToken.php ;
protected $except = [
'/post_request',
];