[英]Response for preflight has invalid HTTP status code 400 Web API MVC
[英]how to get the response api with status code is 400?
I consume api and when the api returns 200 status code, I return the response, but when the api returns 400 status code the api returns array with the errors, my questions is how can i get this array errors and return this array.
編碼
try {
const config = {
method: 'get',
url: 'http://localhost:4000/api/orders',
headers: {'Key': '96db259b-2239-4abb-9b9d-a682a1de6b3c'}
}
const result = await axios(config)
return result.data
} catch (error) {
console.log('error ' + error)
returns result.data.errors
}
這個狀態碼的響應是 400。
"errors": [
{
"value": "96db259b-2239-4abb-9b9d-a682ssa1de6b3c",
"msg": "the API key 96db259b-2239-4abb-9b9d-a682ssa1de6b3c is invalid",
"param": "key",
"location": "headers"
}
]
你可以這樣做
try {
const config = {
method: 'get',
url: 'http://localhost:4000/api/orders',
headers: {'Key': '96db259b-2239-4abb-9b9d-a682a1de6b3c'}
}
const result = await axios(config)
if(result.status != 200) {
throw new Error(`[Status ${result.status}] Something went wrong `);
}
return result.data
} catch (error) {
console.log('error ' + error)
returns error.message;
}
只需要調用error的message屬性,例如:
try {
const config = {
method: 'get',
url: 'http://localhost:4000/api/orders',
headers: {'Key': '96db259b-2239-4abb-9b9d-a682a1de6b3c'}
}
const result = await axios(config)
return result.data // this only called with success response, status code 200
} catch (error) {
console.log('error ' + error)
returns error.message;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.