使用javascript从服务器获取字符串响应

使用javascript从服务器获取字符串响应,javascript,Javascript,我有一个fetch()方法,它向Web Api 2服务器发出post请求。如果服务器认为POST数据有效,则返回一个字符串,其中包含字符串格式的登录令牌。问题是,当我尝试应用response.json时,我无法获取fetch方法中返回的字符串,而在分派response.json之前,我无法获取console.log中返回的字符串 这是我的获取代码: export const login = (user) => (dispatch) => { dispatch({ type: 'LOG

我有一个fetch()方法,它向Web Api 2服务器发出post请求。如果服务器认为POST数据有效,则返回一个字符串,其中包含字符串格式的登录令牌。问题是,当我尝试应用response.json时,我无法获取fetch方法中返回的字符串,而在分派response.json之前,我无法获取console.log中返回的字符串

这是我的获取代码:

export const login = (user) => (dispatch) => {
dispatch({ type: 'LOGIN_STARTED' });
return fetch(`${httpApiUrl}/api/userdata/verify`, {
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify(user)
})
    .then((response) => {
        if (!response.ok) {
            Alert.alert('ERROR', 'User or password is incorrect');
            dispatch({ type: 'LOGIN_FAILED', data: 'User or password is incorrect' });
        }
        else return response;
    }).then((response) => response.json).then((response) => {
        dispatch({ type: 'LOGIN_SUCCEEDED', data: response.json });

    })
    .catch(error => {
        dispatch({ type: 'LOGIN_FAILED', data: error.message });
    });
};
这是我的服务器post方法代码

[HttpPost]
[Route("verify")]
[ResponseType(typeof(string))]
[AllowAnonymous]
public HttpResponseMessage VerifyUser(HttpRequestMessage request, UserDataView data)
{
    return CreateHttpResponse(request, () =>
    {
        HttpResponseMessage response = null;
        if (service.wasUpdated(data)) //de fapt verifica daca e userul in baza de date
        {
            response = request.CreateResponse(HttpStatusCode.OK, JwtManager.GenerateToken(data.username));
        }
        else
            response = request.CreateResponse(HttpStatusCode.OK, false);
        return response;
    });
}
我在服务器上检查了函数是否返回了字符串,它确实返回了,字符串被添加到响应中

我怎样才能进地铁 分派({type:'LOGIN_successed',data:response.json});
数据中的方法my string response和response.json不再被识别?谢谢。

响应是否用
”{type:'LOGIN_successed',data:response.json}“
包装在控制台中?response.json()代替?response.json()为函数提供了一个未识别的错误。而且响应也没有包装在“”中,如果我在分派之前在控制台中打印它,在与它相同的函数中,它显示我未定义。如果您的响应是纯字符串,而不是JSON对象,则预期响应类型是文本,而不是JSON