【问题标题】:When I use axios with Nuxt.js for API communication, I get an error saying 502 (Bad Gateway)当我使用带有 Nuxt.js 的 axios 进行 API 通信时,我收到一条错误消息 502 (Bad Gateway)
【发布时间】:2021-05-05 02:13:52
【问题描述】:

实现了一个新的注册屏幕,开发语言是 Nuxt.js,数据库是 AWS / DynamoDB。 这次想用axios的post方法在DynamoDB中注册一个用户名、邮箱和密码,但是报了502错误。

代码如下。

↓nuxt.config.js

  axios: {
      proxy: true
    },
  proxy: {
    '/regist': {
      target: 'requestURL',
      pathRewrite: {
        '^/regist': '/'
      }
    },

↓signup.vue


<!-User name / email address / password input screen->
    <div class="container">
      <validation-observer v-slot="{ invalid }">
        <div class="wrapper">
          <div>
            <div class="user_name">
              <p>UserName:</p>
              <text-field-with-validation v-model="username" rules="required" fieldname="UserName" />
            </div>
            <div class="mailaddress">
              <p>Mailaddress:</p>
              <text-field-with-validation v-model="mailaddress" rules="email" fieldname="Mailaddress" />
            </div>
            <div class="password">
              <p>Password:</p>
              <text-field-with-validation v-model="password" rules="required" fieldname="Password" />
            </div>
          </div>
          <button type="submit" class="registration-button" :disabled="invalid" @click="signUp">Regist</button>
        </div>
      </validation-observer>
    </div>

<script>
import TextFieldWithValidation from "@/components/TextFieldWithValidation";

export default {
  data: function() {
    return {
      username: '',
      mailaddress: '',
      password: ''
    };
  },
  methods: {
    signUp(){
      this.$axios.post('/regist',
      {
        m_users: {
            user_name: this.username,
            mailaddress: this.mailaddress,
            password: this.password
        }
      }
      )
      .then(res => {
        console.log(res);
      })
      .catch(() => {
        alert('✖︎✖︎✖︎');
      })
    },
  }
};
</script>

为了避免CORS限制,在nuxt.config.js中安装了proxy。 我有麻烦,因为我不知道原因。 如果你能告诉我如何解决它,我将不胜感激。

【问题讨论】:

    标签: proxy amazon-dynamodb nuxt.js


    【解决方案1】:

    502 错误很可能是 server-side error。您能否使用Postman 之类的应用向您的 API 发出相同的请求?

    【讨论】:

    • 我从未使用过 Postman。请求似乎已发送,但内容似乎为空。使用 axios 进行 API 通信有错吗?
    • 当我用 Postman 尝试时,它被显示出来了。
    • 要检查的变量太多了。我会首先通过POSTing/GETing 到JSONPlaceholder 来消除您将axios 作为变量的使用,确保您得到正确的响应。如果是,那么您很可能会在服务器端或您的请求正文中遇到错误。
    猜你喜欢
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多