#52 Fix api request error handler

Merged
rogerluo410 merged 1 commits from feat/add_try_page into master 1 year ago
  1. +23
    -10
      pages/tryit/index.vue
  2. +0
    -1
      plugins/axios.js

+ 23
- 10
pages/tryit/index.vue View File

@@ -54,7 +54,10 @@
<div class="request-panel">
<div class="request-url">
<p>
结果: <span>状态码 {{ response_status }}</span>
结果:
<span v-show="response_status"
>状态码 {{ response_status }}</span
>
</p>
</div>
</div>
@@ -76,6 +79,8 @@
</template>

<script>
import Axios from 'axios'

export default {
layout: 'tryit',
data() {
@@ -118,17 +123,25 @@ export default {
}
},
async submitRequest() {
console.log(this.request_method)
this.loading = true
const method = 'post'
const res = await this.$axios[method](this.request_url, this.jsonCode, {
token: this.token,
})
const method = this.request_method.toLowerCase()
try {
const res = await Axios[method](this.request_url, this.jsonCode, {
headers: {
token: this.token,
},
})

this.loading = false
console.log(res)
this.jsonResultCode = JSON.stringify(res.data)
this.response_status = res.status
this.loading = false
this.jsonResultCode = JSON.stringify(res.data)
this.response_status = res.status
} catch (error) {
if (error.response) {
this.loading = false
this.jsonResultCode = JSON.stringify(error.response.data)
this.response_status = error.response.status
}
}
},
},
}


+ 0
- 1
plugins/axios.js View File

@@ -25,7 +25,6 @@ export default function ({ $axios, $config: { baseUrl } }) {
// alert(error.response.data.errors[0].message)
// }
// eslint-disable-next-line no-console
console.log(error)
const code = parseInt(error.response && error.response.status)
if (code === 401) {
// eslint-disable-next-line no-console


Loading…
Cancel
Save