2017-06-21 27 views
0

我想實現http DELETE。該應用程序是用PHP和slim3框架編寫的。前端是角形2.超薄3 Http刪除方法不起作用

如果模式看起來像:$slimApp->delete('/delete', ...)一切都很好。

只要我喜歡介紹PARAMS:$slimApp->delete('/delete/{id}', ...)我得到以下錯誤:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我已經閱讀文檔https://www.slimframework.com/docs/cookbook/enable-cors.html但卻我無法得到它的工作。

這裏是我的中間件:

<?php 

class MyMiddleware { 

    public function __invoke(Request $req, Response $res, $next) { 
     $res->withHeader('Access-Control-Allow-Origin', 'http://localhost:8100') 
      ->withHeader('Access-Control-Allow-Credentials', 'true') 
      ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') 
      ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 

     return $next($req, $res); 
    } 

} 

$app = new \Slim\App([ 
    "settings" => [ 
     "determineRouteBeforeAppMiddleware" => true 
    ] 
]); 

$app->add(new MyMiddleware()); 

任何想法可能是什麼問題呢?

+0

您能爲此路線顯示回撥嗎? –

回答

0

好的解決了這個問題。 .htaccess只是不讓請求「到達」代碼。所以我只需要優化規則。

1

響應是不可變的,重新分配變量。

public function __invoke(Request $req, Response $res, $next) { 
    $res = $res->withHeader('Access-Control-Allow-Origin', 'http://localhost:8100') 
     ->withHeader('Access-Control-Allow-Credentials', 'true') 
     ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') 
     ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); 

    return $next($req, $res); 
} 
+0

仍然無法正常工作...記住:沒有參數在它正在工作的網址。 – user3481997