hprose / hprose-js

Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
MIT License
135 stars 44 forks source link

setHeader跨域 #8

Closed muunnw closed 6 years ago

muunnw commented 6 years ago

客户端使用了setHeader('Access-Token','sfasdfad'); 服务端也设置了$server->crossDomain=true; 调用的时候还是提示跨域问题:

Request header field Access-Token is not allowed by Access-Control-Allow-Headers in preflight response.

andot commented 6 years ago

你这个是跨域加自定义头,你需要在服务器端加上 Access-Control-Allow-Headers: Access-Token

muunnw commented 6 years ago

我使用Hprose\Swoole\Http\Server来作为服务器端,这个应该怎样加上Access-Control-Allow-Headers: Access-Token?

andot commented 6 years ago
$server->onSendHeader = function($context) {
    $context->server->header('Access-Control-Allow-Headers', 'Access-Token', $context);
};
muunnw commented 6 years ago

嗯,可以了,原来header还有第三个参要传。。。感谢!

andot commented 6 years ago

嗯,这是为了在不同实现的服务器下,可以用通用的方式来发送 header 才这样设计的。