【问题标题】:submitting form and calling node.js using axios使用 axios 提交表单并调用 node.js
【发布时间】:2021-08-23 14:27:36
【问题描述】:

我有这个页面,它的作用是在表单提交时使用 axios 调用后端函数/updateServices

 import '../../../styles/services.css';
    import axios from 'axios'
    import React, { Component } from 'react';
    
    class AddServicesPage extends Component {
        constructor(props) {
          super(props);
      
          this.state = {
            siteLocation: '',
            Services: '',
            date: '',
            cnum: ''
          };
        }
      
        handleInputChange = e => {
          this.setState({
            [e.target.name]: e.target.value,
          });
        };
      
        handleSubmit = e => {
          e.preventDefault();
      
          var date = Date().toLocaleString()
          const { siteLocation, Services, cnum } = this.state;
      
          const selections = {
            site: siteLocation,
            content: Services,
            updatedByCNUM: cnum,
          };
      
          axios
            .post(`/updateServices`, selections) 
              .then(() =>
                      console.log("updating"),
                      window.location = "/admin/services"
              )
              .catch(function(error) {
                  // alert(error)
                  window.location = "/admin/services/new"
              })
        };
      
      
    
        render() {
        return (
            <div className="App">
               <div id="form-main">
                <div id="form-div">
                <form onSubmit={this.handleSubmit} className="form">
                      
                    <select name="siteLocation" id="siteLocation" onChange={this.handleInputChange} >
                        <option value="OTT">Ottawa</option>
                        <option value="MAR">Markham</option>
                    </select>
    
                    <p className="email">
                        <input name="Services" type="text" className="validate[required,custom[email]] feedback-input" id="Services" placeholder="Services" onChange={this.handleInputChange} />
                    </p>
                  
                   <p className="cnum">
                        <input name="cnum" type="text" className="validate[required,custom[email]] feedback-input" id="cnum" placeholder="cnum" onChange={this.handleInputChange} />
                    </p>
                    
                    
                    
                    <div className="submit">
                        <input type="submit" value="SEND" id="button-blue"/>
                        <div className="ease"></div>
                    </div>
                    </form>
                    <a href="/tour">Get Results{'->'}</a>
                </div>
            </div>
            </div>
        );
        }
    }
    
    export default AddServicesPage;

然而,它甚至没有击中表单提交的后端,而是跳过它并转到.then 说它正在工作。在后端,在我的/updateServices 中,它甚至不记录任何内容。它什么也不记录,甚至没有空行,这意味着它永远不会到达后端。我的代码有什么问题吗?

【问题讨论】:

  • 您确定您的后端 API 端点 URL 设置正确吗?在上面的 sn-p 中,您只在 API 调用中调用 /updateServices,是否为 axios 配置了正确的 API 域?如果那是肯定的,那么我认为您必须检查您的后端路由,它可能会比您预期的更早被拦截并且只回复200 OK,另外作为一个附加提示,请考虑使用 React 路由器路由您的 React 应用程序,执行不要使用 window.location 因为它总是会刷新你的页面并且会丢失你的 React App 上下文。
  • 你的前端和后端是否在同一个端口上运行?
  • @iunfixit 后端为 3001,前端为 3000
  • @HasinthaAbeykoon thx 4 提示。这很奇怪,因为它今天刚刚停止工作。从未遇到过这个问题,也没有更改代码。

标签: node.js reactjs axios


【解决方案1】:

您似乎没有正确设置axios 调用中的路径。

您调用/updateServices 时没有指向任何域。

axios.post(`/updateServices`, selections) 

您应该传递一个域,然后传递一个路由。例如,如果您的后端在 localhost 的 5000 端口上运行,那么您的调用应该如下所示

axios.post(`http://localhost:5000/updateServices`, selections) 

如果您想以必须在代码中输入的方式使用 axios,您可以像下面的示例那样进行:

const backend = axios.create({baseURL: 'http://localhost:5000'});
backend.post(`/updateServices`, selections)

如果我必须对同一个域进行多次调用,我会使用这种模式。另外,我会将axios create 放在一个单独的文件中,并在需要的地方从那里导入。

【讨论】:

  • 感谢 4 的回答。但是在后端,我定义了一个端口,并且在每次调用之前都会命中该端口。但是,我把它放在你展示的所有方式中,只是为了测试它,它仍然不起作用。很奇怪。
  • 在上面的示例中尝试使用127.0.0.1 而不是localhost。您能否分享您更新的代码(如果此代码不起作用)以及您的服务器正在运行的端口?
  • 好的,所以把它们放进去,它消除了代理的错误。错误是,无法从 3000 发送到 3001 之类的。但它仍然不会发送到后端。我无法从我的 node.js 文件中获得任何发布请求。后端运行3001,前端运行3000
  • 另外,我在 package.json 中将代理设置为 localhost:3001,所以它在那里与之对话。我认为这部分很好,因为它一直在工作,但奇怪的是我现在无法在后端达到任何请求。我尝试通过 post 使用 axios 调用的任何路径都失败了。失败我的意思是甚至没有击中它
  • 很好的答案,如果你尝试过这个,我宁愿考虑修改我的后端路由,因为通常路由是按顺序拦截的,我最好的猜测是你的端点在你的预期之前被另一个路由匹配拦截一。如果您可以向我们展示您的路由/端点是如何在后端服务器中配置的,我认为可以提供更多帮助!
猜你喜欢
  • 2019-11-01
  • 2020-11-03
  • 2020-03-05
  • 2018-08-16
  • 2012-01-27
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多