简体   繁体   English

将 axios 和 axios-mock-adapter 用于 onSubmit 事件

[英]Use axios and axios-mock-adapter for onSubmit event

I'm trying to use axios and axios-mock-adapter in one place to aggregate more mocks and then export the axios instance to use it wherever I want:我试图在一个地方使用 axios 和 axios-mock-adapter 来聚合更多模拟,然后导出 axios 实例以在我想要的任何地方使用它:

axios.js axios.js

import axios from 'axios';

let api = axios.create({
    baseURL: 'http://localhost:3500',
});

export default api

Login.js登录.js

import api from '../../../api/axios';
import MockAdapter from 'axios-mock-adapter';
let mock = new MockAdapter(api);
const LOGIN_URL = '/users';

const onSubmit = async data => {

 console.log(data);

 const user = data.loginEmail;
 const pwd = data.password

 const response = await mock.onPost(LOGIN_URL,
  JSON.stringify({ user, pwd }),
  {
    headers: { 'Content-Type': 'application/json' },
    withCredentials: true
  }
 );

console.log(response);
}

But I get this response:但我得到这样的回应:

{reply: ƒ, replyOnce: ƒ, passThrough: ƒ, abortRequest:ƒ,abortRequestOnce: ƒ, …}abortRequest: ƒ ()abortRequestOnce: ƒ()networkError: ƒ ()networkErrorOnce: ƒ ()passThrough: ƒ passThrough()reply: ƒ reply(code, response, headers)replyOnce: ƒ replyOnce(code, response, headers)timeout: ƒ ()timeoutOnce: ƒ ([[Prototype]]: Object

Can anyone help me?谁能帮我? What did I miss?我错过了什么?

It sends status 200 but it doesn't send any request to the server and it's like not connecting to the server它发送状态 200 但它不向服务器发送任何请求,就像没有连接到服务器一样

It looks like you're trying to mock a GET request to the '/users' endpoint with the axios-mock-adapter, but in your onSubmit function you are making a POST request to the same endpoint.看起来您正在尝试使用 axios-mock-adapter 模拟对“/users”端点的 GET 请求,但在您的 onSubmit function 中,您正在向同一端点发出 POST 请求。 That's why you're getting a 404 error.这就是您收到 404 错误的原因。 You can either change the mock.onGet to mock.onPost or change the onSubmit function to make a GET request.您可以将 mock.onGet 更改为 mock.onPost 或将 onSubmit function 更改为发出 GET 请求。 Also, you should check the baseURL that you are using in the axios.create() method in axios.js and make sure it's the correct one.此外,您应该检查 axios.create() 方法中使用的 baseURL axios.js 并确保它是正确的。 Also, check if you have setup your server properly and it's running on the correct port.此外,请检查您是否已正确设置服务器以及它是否在正确的端口上运行。 If you continue to have issues you might want to check the browser's devtools.network tab and check the request being made and the corresponding response.如果问题仍然存在,您可能需要检查浏览器的 devtools.network 选项卡并检查发出的请求和相应的响应。

axios.js axios.js

import axios from 'axios';

let api = axios.create({
    baseURL: 'http://localhost:3500',
});

export default api

The solution was about to change the Login.js解决方案即将更改 Login.js

// ** API
import api from '@api';
const LOGIN_URL = '/auth'

And onSubmit Event和 onSubmit 事件

  // !! On Submit Event
  const onSubmit = async data => {
    const user = data.username;
    const pass = data.password;

    try {
      const response = await api.post(LOGIN_URL,
        JSON.stringify({ user, pass }),
        {
            headers: { 'Content-Type': 'application/json' },
            withCredentials: true
        }
      );
      .
      .
      .
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM