search

Home  >  Q&A  >  body text

javascript - How to intercept ajax requests sent by `axios` in nodejs?

After a general front-end library sends an ajax request, the response object headers will have X-Requested-With:XMLHttpRequest
but## The ajax request sent by #axios does not have this field in the response header

How to intercept the front-end request sent by

axios in nodejs code?

巴扎黑巴扎黑2807 days ago929

reply all(3)I'll reply

  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 10:38:01

    I want to add X-Requested-With header to every request:

    axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

    To intercept the sent ajax, just use spy to replace XMLHttpRequest with the original method.

    I can’t figure out why axios doesn’t add this header by default. . .

    reply
    0
  • 巴扎黑

    巴扎黑2017-07-05 10:38:01

    Add proxy plug-in to intercept it

    reply
    0
  • 天蓬老师

    天蓬老师2017-07-05 10:38:01

    Are you sure it's an axios problem? Have you tried using another tool? Ajax and axios are just tools for transmitting http requests, so I want to point out that the ajax request sent by the poster's axios is wrong. In addition, I don't know your backend. How to write it? If no acceptable request header is configured when crossing domains, the specified header data will not be obtained. "Access-Control-Allow-Headers": "X-Requested-With"

    reply
    0
  • Cancelreply