提取API调用捕获SyntaxError:JSON输入意外结束

时间:2018-07-10 13:37:35

标签: reactjs jwt

我在使用React进行JWT身份验证时遇到问题。 我将POST发送到后端服务器并获得状态200的响应。但是,登录后,我的fetch()捕获到错误SyntaxError: Unexpected end of JSON input

loggedIn()返回正确的结果。它具有true和false值。

 fetch(url, options) {
        // performs api calls sending the required authentication headers
        const headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

        // Setting Authorization header
        // Authorization: Bearer xxxxxxx.xxxxxxxx.xxxxxx
        if (this.loggedIn()) {
            headers['Authorization'] = 'Bearer ' + this.getToken()
        }

        return fetch(url, {
            headers,
            ...options
        })
            .then(this._checkStatus)
            .then(response => response.json())
    }

    _checkStatus(response) {
        // raises an error in case response status is not a success
        if (response.status >= 200 && response.status < 300) { // Success status lies between 200 to 300
            return response
        } else {
            var error = new Error(response.statusText)
            throw error
        }
    }

也许有人有什么想法吗?

0 个答案:

没有答案