在IE中返回的字符无效,但在Firefox和Chrome

时间:2018-03-15 12:29:17

标签: ajax reactjs internet-explorer asp.net-core

我正在使用fetch将JSON有效负载返回到React SPA。我的Web服务器后端是ASP.NET Core 2.0。我最近更新到ASP.NET Core 2.0并且在我的生活中无法弄清楚为什么IE不再适用于Web应用程序。

获取很简单。

fetch(getApiPath() + url, {
    credentials: 'include'
})
.then(function(response){
    if (response.status === 401 && history) {
        history.push("/login")
        throw new Error('Redirecting to login');
    } else if (response.status === 200) {
        return response.json();
    } else {
        throw new Error(response.statusText);
    }
})
.then(success)
.catch(function(e) {
    console.log(e)
});

服务器端也非常简单。

    [Authorize]
    [Route("/dashboard")]
    public object Index()
    {
        Log.Debug("Index");
        return new { dashboard = _dashboard, authenticated = HttpContext.User.Identity.IsAuthenticated };
    }

问题表现在IE中的“无效字符”错误中。这在Chrome和Firefox中运行良好。在查看响应正文时,IE响应实际上是无效字符,而在Chrome中,它是JSON有效负载。

enter image description here

我有点不确定在哪里开始研究IE为什么不能正确接收或解析HTTP响应。有任何想法吗?

编辑:

从运行在端口10000上的Webpack Dev Server发送跨源请求到运行在10001上的本地ASP.NET Core应用程序。打包部署时,React App和ASP.NET Core App都运行在10000上。

两个请求之间的标头。

IE请求

enter image description here

IE回复

enter image description here

enter image description here

更新了端点以返回IActionResult并显式返回JSON。结果相同。我也意识到它在Edge中也不起作用。

    [Authorize]
    [Route("/dashboard")]
    public IActionResult Index()
    {
        return Json(
            new { dashboard = _dashboard, authenticated = HttpContext.User.Identity.IsAuthenticated }
        );
    }

1 个答案:

答案 0 :(得分:0)

如果没有其他信息,我怀疑该问题与ASP.Net content negotiation有关,而且您的方法返回类型为object。不要使用object,这不是Java:))

在其他任何事情之前,请确保fetch正在IE中发送Accept: application/json标头。

我还建议您将控制器方法的返回类型更改为IActionResult(如果要强制使用JSON,则为JSONResult