【问题标题】:vue form axios post not submittingvue form axios post未提交
【发布时间】:2020-07-18 17:56:45
【问题描述】:

我的 fom 有以下代码

<form @submit="onSubmit" method="POST">
  <div class="mb-4">
    <label class="block" for="name">
      <span class="text-gray-900">Full name</span>
      <input
        class="form-input mt-1 block w-full outline-none"
        placeholder="Sherlock Holmes"
        v-model="formData.name"
        required
        id="name"
        name="name"
        type="text"
      />
    </label>
  </div>

  <div class="mb-4">
    <label class="block" for="email">
      <span class="text-gray-900">Email address</span>
      <input
        class="form-input mt-1 block w-full outline-none"
        placeholder="sherlock@bakerstreet.com"
        v-model="formData.email"
        required
        id="email"
        name="email"
        type="email"
      />
    </label>
  </div>

  <div class="mb-4">
    <label class="block" for="department">
      <span class="text-gray-700">Department</span>
      <select
        class="form-select mt-1 block w-full outline-none"
        name="deptid"
        v-model="formData.deptid"
      >
        <option value="1">General Support</option>
        <option value="5">Billing Support</option>
        <option value="2">Hosting Support</option>
        <option value="3">Domain Support</option>
        <option value="4">Game Support</option>
      </select>
    </label>
  </div>

  <div class="mb-4">
    <label class="block" for="message">
      <span class="text-gray-900">Message</span>
      <textarea
        class="form-input mt-1 block w-full outline-none h-56 resize-none"
        placeholder="Enter your message here"
        v-model="formData.message"
        name="message"
        id="message"
        cols="30"
        rows="10"
        required
      />
    </label>
  </div>

  <div class="flex mt-6">
    <label class="flex items-center">
      <input type="checkbox" class="form-checkbox" />
      <span class="ml-2">
        I agree to the
        <a href="#" class="underline">privacy policy</a>
      </span>
    </label>
  </div>

  <div class="flex mt-6">
    <label class="flex items-center">
      <button
        type="submit"
        name="ticketSubmit"
        value="Submit"
        class="flex items-center py-3 px-6 border rounded bg-gray-800 hover:bg-gray-900 font-medium text-white transition duration-300 ease-in-out"
      >
        <i class="fas fa-paper-plane mr-4"></i>
        Send
      </button>
    </label>
  </div>
</form>

还有以下script

<script>
import axios from "axios";
import FlashMessage from "../components/FlashMessage";
import FeaturesWGrid from "../components/FeaturesWGrid";
import Grid from "../components/Grid";
import GridItem from "../components/GridItem";

export default {
  name: "Contact",
  components: {
    FlashMessage,
    FeaturesWGrid,
    Grid,
    GridItem
  },
  data() {
    return {
      showMsg: false,
      formData: {
        name: "",
        email: "",
        deptid: "1",
        message: ""
      }
    };
  },
  methods: {
    onSubmit() {
      axios
        .post("https://hostedcarbon.com/backend/contact.php", this.formData)
        .then(res => {
          console.log(res);
        })
        .catch(err => {
          console.error(err);
        });

      this.showMsg = true;
      setTimeout(() => {
        this.showMsg = false;
      }, 3000);
    }
  }
};
</script>

如果我执行以下&lt;form action="/backend/contact.php" method="POST"&gt;,则表单有效,因此contact.php 有效。所以我不确定为什么 axios 帖子不起作用。

这里是 PHP 代码减去 API 密钥(有效的)

<?php
    $whmcsUrl = "https://billing.hostedcarbon.com/";
    $api_identifier = "";
    $api_secret = "";
    $name = "";
    $email = "";
    $deptid = "";
    $message = "";

    if (isset($_POST['name'])) {
        $name = $_POST['name'];
    }

    if (isset($_POST['email'])) {
        $email = $_POST['email'];
    }

    if (isset($_POST['deptid'])) {
        $deptid = $_POST['deptid'];
    }

    if (isset($_POST['message'])) {
        $message = $_POST['message'];
    }

    // Build post values
    $postfields = array(
        'action' => 'OpenTicket',
        'identifier' => $api_identifier,
        'secret' => $api_secret,
        'deptid' => $deptid,
        'name' => $name,
        'email' => $email,
        "subject" => "Contact form Inquiry from $name",
        'message' => $message,
        'priority' => 'Medium',
        'useMarkdown' => 'false',
        'responsetype' => 'json',
    );

    // Call the API
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $whmcsUrl . 'includes/api.php');
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
    $response = curl_exec($ch);
    if (curl_error($ch)) {
    die('Unable to connect: ' . curl_errno($ch) . ' - ' . curl_error($ch));
    }
    curl_close($ch);

    // Decode response
    $jsonData = json_decode($response, true);

    // Dump array structure for inspection
    var_dump($jsonData);
    var_dump($_POST)
?>

【问题讨论】:

  • 你尝试过 axios.post("hostedcarbon.com/backend/contact.php", this.formdata).then(res=>{console.log(res}).catch(err=>console.log (错误)并检查是否做某事?
  • 我已经尝试过了,但控制台中什么也没有出现我确实有 "no-console": "off", 作为 eslint 规则,如果这是一个问题,因为我在构建时收到 unexpected console statement 错误。 @NBlack
  • 检查 Mustafa 解决方案,可能是大写问题。
  • 更新了 Q 并在 Mustafa 的回答中添加了一些 cmets @NBlack

标签: javascript vue.js axios


【解决方案1】:

您调用的是 this.formdata 而不是 this.formData? 编辑: 注意:你正在使用

onSubmit: function () {}

不要使用普通函数,因为这里的 this 指的是当前函数 使用 ES6 函数

onSubmit() {}

编辑:

var form = new FormData()

form.append('name', this.formData.name)
form.append('email', this.formData.email)
form.append('deptid', this.formData.deptid)
form.append('message', this.formData.message)

  axios.post("https://hostedcarbon.com/backend/contact.php", form)
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.error(err);
  });

【讨论】:

  • 啊,那是我的错,改变了。我可以提交表单,页面刷新,但如果您想测试表单和 PHP 文件链接是 hostedcarbon.com/contact 并且文件链接是 hostedcarbon.com/backend/contact.php,仍然没有提交任何内容
  • 也许我需要@submit.prevent="" 代替
  • OK 更新,我添加了 submit.prevent,现在我在控制台中收到了回复,我收到了 response: "array(2) {↵ ["result"]=&gt;↵ string(5) "error"↵ ["message"]=&gt;↵ string(51) "Name and email address are required if not a client"↵}↵array(0) {↵}↵"
  • 看来数据没有被传递,但您可以使用上面的链接尝试表单。我将 PHP 代码添加到问题中
  • 但如前所述,如果我只是在表单标签上调用操作,票证就会创建。
猜你喜欢
  • 1970-01-01
  • 2019-05-12
  • 2018-09-27
  • 2018-12-24
  • 1970-01-01
  • 2010-12-06
  • 2013-07-30
  • 1970-01-01
  • 2019-06-18
相关资源
最近更新 更多