axios在20秒内给出响应,但是具有相同请求有效负载的相同api在6秒内给出来自邮递员的响应 [英] axios is giving response in 20 sec, but same api with same request payload is giving response in 6 sec from postman

查看:56
本文介绍了axios在20秒内给出响应,但是具有相同请求有效负载的相同api在6秒内给出来自邮递员的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用axios进行来自reactjs框架的api调用.通过axios,我会在20秒内收到响应,但是当我从邮递员发出相同的请求时,它会在6秒内给出响应.

i'm using axios for api calls from reactjs framework. by axios i'm getting response in 20sec, but when i make the same request from postman it is giving response in 6 secs.

我通过使用提取尝试了相同的请求,这也花费了大约20秒的时间

i tried the same request by using fetch, it is also taking around 20 secs

     axios.post(URL, reqObject, {
            headers: {
                'Access-Control-Allow-Origin': '*',
                'Authorization': 'Bearer '.concat(AccessToken)
            },
        }
        ).then(function (response) {

            console.log(" axios  APi call response ", response.data.value)

        }).catch(function (error) {
            console.log("  error error");
            console.log(error);
        });

推荐答案

听起来这是对响应缓慢的服务器的跨域请求.跨域POST需要在浏览器上执行称为预检" OPTIONS请求的操作.如果服务器响应缓慢,则可能需要很长时间才能响应OPTIONS请求,然后又需要很长时间才能响应POST.

It sounds like this is a cross-origin request to a slowly-responding server. Cross-origin POSTs require something called a "preflight" OPTIONS request on browsers. If the server is responding slowly, it may take a long time to respond to the OPTIONS request, and then a long time again to respond to the POST.

相比之下,由于邮递员不是浏览器,因此它不必担心CORS,并且可以在不发送预检选项的情况下发送POST,因此只会产生POST的费用.

In contrast, since postman isn't a browser, it doesn't have to worry about CORS and can send the POST without sending the preflight OPTIONS, so only incurs the cost of the POST.

((如果您想知道为什么邮递员不必进行预检,而浏览器却可以,我建议您仔细阅读同一来源政策在MDN上的CORS,以及我链接到的问题的答案评论.)

(If you're wondering why postman doesn't have to do the preflight but a browser does, I suggest reading through the Same Origin Policy, CORS on MDN, and the answers to the question I linked in a comment.)

这篇关于axios在20秒内给出响应,但是具有相同请求有效负载的相同api在6秒内给出来自邮递员的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆