vendredi 21 août 2015

Laravel5 CORS: No 'Access-Control-Allow-Origin'

Even though I added the CORS filter like below and response header has 'Access-Control-Allow-Origin:*', still 'No Access-Control-Allow-Origin' occurs. What should I do next?

CORS Error

XMLHttpRequest cannot load http://ift.tt/1TXve9B. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.33.10' is therefore not allowed access. The response had HTTP status code 401.

CORS Filter

<?php namespace App\Http\Middleware;
use Closure;

use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Http\Response;

class CORS implements Middleware {

 /**
  * 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', 'POST, GET, OPTIONS, PUT, DELETE')
          ->header('Access-Control-Allow-Headers', 'Content-Type, Accept, Authorization, X-Requested-With');
 }
}

JavaScript

$(document).ready(
  function() {
    $.ajax({
        type: 'POST',
        url: 'http://ift.tt/1TXve9B',
        dataType: 'json',
        data: {
            'id': 'xxx',
            'column': 'yyy',
        }
    })
    .done(function( json ) {
        console.log(json);
    })
    .fail(function(data){
        console.log(data.status);
    });
  }
);

Response Header

Access-Control-Allow-Headers:Content-Type, Accept, Authorization, X-Requested-With
Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Origin:*



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1HZkZFe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire