如何使用 ReactJS Fetch 从 Net Framework 4.5 中的 HttpResponseMessage 获取数据

问题描述

这很简单,但我的解决方案既不使用 Axios,也不使用 .Net Core,因此很难找到答案。

基本上,这是一个 Net Framework 项目的 API 操作:

public HttpResponseMessage GetUser(string nummer)
        {
            try
            {
                User user = new User
                {
                    Name = "name",Domain = "dom"
                };
// Necessary code otherwise I get an error
                var configuration = new System.Web.Http.HttpConfiguration();
                var request = new HttpRequestMessage();
                request.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
                return request.CreateResponse(
                    System.Net.HttpStatusCode.OK,user);
            }
            catch (ApiException e) { }
        }

还有我的 React:

fetch('./User/GetUser?nummer=' + nr,{
            method: 'GET',headers: new Headers({
                'Content-Type': 'application/json'
            })
        })
            .then(res => {
                if (res.ok) {
                    console.log(res);
                    res.json()
                        .then(data => {
                            console.log(data);
                        })
                }
            })

我可以在 res 中获取状态代码和所有内容,但不能在我的响应中获取用户预期的内容。 使用 res.json() 会产生错误。这里有什么问题?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)