目录
1.现象
启动服务后本机可以获取接口数据,存在其他机器接口得不到数据;
2.浏览器调试
Cross-origin resource Sharing error: WildcardOriginNotAllowed
操作: F12->Network->status (悬停显示提示)
3.问题原因:
响应,头结构中“access-control-allow-origin"使用了通配符“*”;
4.解决办法:
根据请求Origin设置access-control-allow-origin;
TUniquePtr<FHttpServerResponse> uptrHttpServerResp = FHttpServerResponse::Create(JsonString, TEXT("application/json"));
TArray<FString>* ArrayOrg = (TArray<FString>*)Request.Headers.Find(FString(TEXT("Origin")));
if (ArrayOrg)
{
uptrHttpServerResp->Headers.Add(TEXT("access-control-allow-origin"), MoveTemp(*ArrayOrg));
}
else
{
FString StrAccessCtrlAllowOrigin = FString(TEXT("http://127.0.0.1:")) + FString::FromInt(m_SERVER_PORT);//FString(TEXT("*"));//
TArray<FString> ArrayAccessCtrlAllowOrigin = { MoveTemp(StrAccessCtrlAllowOrigin) };
uptrHttpServerResp->Headers.Add(TEXT("access-control-allow-origin"), MoveTemp(ArrayAccessCtrlAllowOrigin));
}