2017-03-09 33 views
0

以我角控制器代碼我的HTTP GET方法不與頭工作,但同樣的請求在郵遞員工作角JS get請求與包頭中表示錯誤CORS

$http({ 
    method: 'GET', 
    url: 'http://localhost:8080/api/profiles', 
    headers: { 
    'Authorization': 'Bearer 78954642-sf25-4sd6-sdf2-99582369d5af', 
    'Content-Type':'text/plain' 
    } 
    }).then(function(success) { 
    console.log(success); 
    }, function(error) { 
    }); 

顯示以下錯誤

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://quotebuilder' is therefore not allowed access. The response had HTTP status code 401. 

我的請求頭

OPTIONS /api/profiles HTTP/1.1 
Host: localhost:8080 
Connection: keep-alive 
Pragma: no-cache 
Cache-Control: no-cache 
Access-Control-Request-Method: GET 
Origin: http://quotebuilder 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 
Access-Control-Request-Headers: authorization 
Accept: */* 
Referer: http://quotebuilder/login 
Accept-Encoding: gzip, deflate, sdch, br 
Accept-Language: en-US,en;q=0.8 

什這裏錯了嗎?請幫我解決問題

+0

您需要從chrome安裝CORS插件,或者應該從API啓用CORS。 – Rajesh

+0

我認爲你可以傳遞響應頭文件,它應該可以工作。當我在我的'response'頭文件中添加'.headers(Access-Control-Allow-Methods「,」GET,POST,DELETE,PUT,OPTIONS「)時,它對我有效,如果這樣做不起作用,那麼編輯你的網頁。 xml文件(如果使用的是tomcat) –

回答

2

你應該在你的服務器端禁用CORS來完成這項工作。當資源請求來自不同域的資源時,資源會創建一個跨源HTTP請求,或者資源本身的服務器端看到的資源爲here。如果您使用的是Spring或其他服務器端Web框架,則需要從那裏禁用它。

+1

是的,它是與CORS有關的問題,你必須在服務器端更改CORS設置,例如,如果你使用的是tomcat,編輯web.xml文件來包含CORS標籤,另一個解決方法是禁用web安全在Chrome上 – playerone

+0

是的,但是如果你打算將它部署到生產服務器上,你不應該使用可能會回來困擾你的黑客@playerone –

+0

你是對的:)他應該禁用Chrome安全性,只適用於開發模式 – playerone