The difference between fetch() request carrying parameters and axios

Requirements: It is necessary to continuously receive the data stream from the backend, but the axios request is packaged and displayed on the interface, so there is no way to use axios. fetch() can be read in blocks.

Axios writes the parameters that need to be carried in the request in {} behind the url.

Writing:

          this.$axios
              .post(url,{})  // {}里面是json数据
              .then(() => {})
              .catch((error) => {
                console.log(error);
              });

But in fetch, adding {JSON} after the url has no effect, and the carried json cannot be seen in the F12 request.

Need to write like this:

 method: "post",
        headers: {
          "Content-type": "application:/x-www-form-urlencoded:charset=UTF-8",
        },
        body: JSON.stringify(data),

The data contains the json parameters you want to carry

Overall it looks like this:

 let data = { request: "stream" }; 
 let url = " /api/serial";
 fetch(url, {
        method: "post",
        headers: {
          "Content-type": "application:/x-www-form-urlencoded:charset=UTF-8",
        },
        body: JSON.stringify(data),
      })
        .then((r) => r.body.getReader())
        .then(f);
    },

Guess you like

Origin blog.csdn.net/weixin_39891473/article/details/128564727