AxiosError:从Vue应用程序在Laravel应用程序上发布请求

pxyaymoc  于 9个月前  发布在  iOS
关注(0)|答案(1)|浏览(116)

我需要Axios的建议,以及如何从域A向域B发送请求。

  • 域A:test.fkbraxy.sk(仅限Vue 3)
  • 域名B:app.fkbraxy.sk(仅Laravel 10)

域名A:

路由API(routes/api.php):

Route::post('post-contact', function (Request $request): SentMessage {
    return Mail::to(Settings::first()->email_receive)->send(new ContactFormMail($request));
});

字符串

域名B

联系人页面上的脚本:

data() {
    useMeta({
      title: 'Kontaktujte nás'
    })
    return {
      form: {
        name: null,
        email: null,
        mobile: null,
        msg: null
      },

      success: false,
      error: false,
      loading: false,
      errors: [],
    }
  },
  methods: {
    postForm() {
      if (!this.checkForm()) return false

      this.loading = true
      this.errors = []

      axios.post('https://app.fkbraxy.sk/public/api/post-contact', this.form)
        .then((res) => {
          this.success = true
          console.log(res)
        })
        .catch((error) => {
          this.success = false
          this.errors.push('Nepodarilo sa spojiť so serverom. Skontrolujte či všetky povinné polia boli správne vyplnené.')
          console.log(error)
        })
        .finally(() => {
          this.loading = false
        })
    },

发送正常,邮件发送无误,但控制台总是报错:

{
    "message": "Request failed with status code 500",
    "name": "AxiosError",
    "stack": "AxiosError: Request failed with status code 500\n    at Wh (https://test.fkbraxy.sk/assets/index-a833590d.js:10:1042)\n    at XMLHttpRequest.g (https://test.fkbraxy.sk/assets/index-a833590d.js:10:4229)",
    "config": {
        "transitional": {
            "silentJSONParsing": true,
            "forcedJSONParsing": true,
            "clarifyTimeoutError": false
        },
        "adapter": [
            "xhr",
            "http"
        ],
        "transformRequest": [
            null
        ],
        "transformResponse": [
            null
        ],
        "timeout": 0,
        "xsrfCookieName": "XSRF-TOKEN",
        "xsrfHeaderName": "X-XSRF-TOKEN",
        "maxContentLength": -1,
        "maxBodyLength": -1,
        "env": {},
        "headers": {
            "Accept": "application/json, text/plain, */*",
            "Content-Type": "application/json"
        },
        "method": "post",
        "url": "https://app.fkbraxy.sk/public/api/post-contact",
        "data": "{\"name\":\"sdfsdf\",\"email\":\"[email protected]\",\"mobile\":null,\"msg\":\"asdas dasd asd asd asd asdasd \"}"
    },
    "code": "ERR_BAD_RESPONSE",
    "status": 500
}


在发送.catch(error)后显示错误
我将感谢任何建议,我为我的英语道歉。

toiithl6

toiithl61#

500是一个后端错误。问题是你的返回语句。

return Mail::to(...)->send(...)

字符串
Mail::send()方法不返回任何内容,因此作为响应返回到前端是无效的。

Mail::to(...)->send(...)

if (Mail::failures()) {
  return Mail::failures();
}

return 'Success';


当然,你可以返回任何消息,或查看,或重定向等。

相关问题