【发布时间】:2018-07-01 21:50:25
【问题描述】:
当我向跨服务器发送 ajax 请求时,我收到此错误 The 'Access-Control-Allow-Origin' header contains multiple values '*, http://localhost.dev',。这是我的 ajax
<script>
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'post',
contentType: 'application/json',
url: "https://api.domain.com/api/v1/register/phone",
headers: {
'x-api-key': 'abc',
'partner-id': 'xyz'
},
data: JSON.stringify({
'somedata': 'xyz'
}),
success: function(result){
console.log(result);
},
error: function(xhr) {console.log(xhr)}
});
});
});
我将 Phalcon 和 nginx 用于 api.domain。我是来自http://localhost.dev 的 ajax。我添加了add_header 'Access-Control-Allow-Origin' 'http://localhost.dev';。这是phalcon代码。
In router.php
$router->add(
'/api/v1/register/phone',
[
'module' => 'api',
'controller' => 'phone',
'action' => 'index',
]);
In controller:
class PhoneController extends ControllerBase{
public function indexAction()
{
$apiKey = $this->request->getHeader('x-api-key');
return $this->sendJson([
'status' => 'Unauthorized',
'message' => $apiKey
], 401);
}
}
【问题讨论】: