簡體   English   中英

symfony2 REST,AngularJs access-Control-Allow-Origin

[英]symfony2 REST, AngularJs access-Control-Allow-Origin

好吧,我很絕望。 我正在使用FOSRestBundle和NelmioCorsBundle

當我嘗試通過不同服務器的角度發布數據時,我仍然收到此錯誤:

XMLHttpRequest cannot load 
http://IP/app_dev.php/api/v1/pages.json. 
No 'Access-    Control-Allow-Origin' header is present on the requested resource.
Origin      'http://127.0.0.1:9000' is therefore not allowed access.



Remote Address:IP:80
Request URL:http://IP/app_dev.php/api/v1/pages.json
Request Method:OPTIONS
Status Code:200 OK
Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8,sk;q=0.6,cs;q=0.4
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:192.168.43.195
Origin:http://127.0.0.1:9000
Pragma:no-cache
Referer:http://127.0.0.1:9000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36

Response Headers
Access-Control-Allow-Headers:X-Requested-With, content-type
Access-Control-Allow-Methods:POST, GET, PUT, DELETE, OPTIONS
Connection:Keep-Alive
Content-Length:573
Content-Type:text/html; charset=iso-8859-1
Date:Sun, 29 Jun 2014 18:07:54 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.7 (Ubuntu)

我想整天解決這個問題......

目前我有:

# CORS OPTIONS (add this too)
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "X-Requested-With, content-type"
</IfModule>

nelmio_cors:
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['X-Custom-Auth']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS']
            max_age: 3600

發送者:

            var data = new FormData;
            data.append('title', 'title');
            data.append('body', 'body');

            var request = $http({
                method: 'POST',
                url: TB+"/app_dev.php/api/v1/pages.json",
                headers: {
                    'Authorization': 'Bearer '+User.getAccessToken(),
                    'Content-Type': 'application/json'
                },
                data: data
            });

知道它有什么問題嗎?

截圖來自2014-06-29 20 20 01

我和NelmioCorsBundle有類似的問題,我解決了這個設置:

nelmio_cors:
    paths:
        '^/api/':
            allow_origin: ['*']
            allow_headers: ['*']
            allow_methods: ['POST', 'PUT', 'GET', 'DELETE','OPTIONS']
            max_age: 3600

如果你使用的是chrome,你應該更好地考慮在localhost ip上工作。 一種常見的方法是將localhost反向代理到像your-domain.com這樣的自定義本地域(為此你必須使用像apache或nginx這樣的web服務器),反向代理到你的127.0.0.1:9000 ip的所有連接,有127.0.1.1 your-domain.com的/ etc / hosts中的別名,只需閱讀symfony文檔,即可將自定義標頭添加到ajax請求中。 我在這里假設你有一個連接到控制器的ajax路由,一個常見的例子是返回一個頭數組作為第三個參數

return new Response($json, 201, array('Access-Control-Allow-Origin' => '*', 'Content-Type' => 'application/json'));

我不會讓自己復雜化並使用我無法控制的第三方插件。 希望這會有所幫助,它在symfony cookbook中有記載( http://symfony.com/doc/current/book/http_fundamentals.html

allow_headers: '*'解決了我的問題:

nelmio_cors:
    paths:
        '^/api/':
            ...
            allow_headers: '*'
            ...

對我來說,錯誤記錄為NelmioCorsBundle拋出的Unauthorized header content-type (我正在使用標准的Angular $ resource + Symfony + NelmioCorsBundle設置)

NelmioCorsBundle默認只允許'accept''accept-language''content-language''origin' (參見https://github.com/nelmio/NelmioCorsBundle/blob/1.4.0/EventListener/CorsListener.php# L32

這里拋出錯誤: https//github.com/nelmio/NelmioCorsBundle/blob/1.4.0/EventListener/CorsListener.php#L158

-

PS:請注意允許任何來源打開的安全漏洞( *

要訪問其他服務器上的資源,該服務器必須在其響應中聲明Access-Control-Allow-Origin標頭,否則由於瀏覽器安全策略而無法工作。 看起來你沒有在你的Simfony應用程序中加載Nelmio。

public function registerBundles()
{
    $bundles = array(
        ...
        new Nelmio\CorsBundle\NelmioCorsBundle(),
        ...
    );
    ...
} 

對我來說,問題出在網絡上......我想......我正在從移動設備創建一個熱點...然后在筆記本電腦上(連接到這個熱點)運行服務器,從移動設備我試圖訪問這個服務器。

因為幾天后我只是設置路由器而沒有CORS的問題。

我們在這里做的最常見的錯誤是我們在客戶端和服務器端不匹配方法類型。 確保在angularjs和symfony路由中都有方法類型為“POST”.. !!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM