fetch异常状态处理

function CheckRequest(response) {
    if (!response.ok) {
        var error = new Error(response.statusText)
        error.response = response
        throw error
    } else {
        return response.json()
    }
}

fetch(URL, {method: 'POST', body: MyData, credentials: 'include'})
    .then(CheckRequest)
    .then(function(ResData) {
        console.log('request succeeded with JSON response', ResData)
    }
    .catch(function(error) {
        console.log('request failed', error)
    })

猜你喜欢

转载自blog.csdn.net/chbxgbin/article/details/89707936