无法将数据写入.json文件

时间:2019-05-22 10:49:45

标签: javascript jquery json ajax

我有一个名为form.json的JSON文件

{
  "name": "John",
  "email": "john@example.com"
}
$(document).ready(function() {
  /* bind to the submit event on the form */
  $('#registerform').submit(function(event) {
    /* get the form input values */
    var name = $(this).children("input[name='name']").val()
    var email = $(this).children("input[name='email']").val()
    console.log(name);
    console.log(email);

    /* send a post request */
    $.post({
      url: 'form.json',
      dataType: "json",
      mimeType: "textPlain",  /* target of POST request */
      data: {
        name: name,
        email: email
      },
      success: function(data) {
        /* assume data in response is a message to show the user */
        $("#usermessage").innerHTML = data.message;
      }
    });
    /* prevent the normal submission of the form */
    event.preventDefault()
  });
});
<form id="registerform">
  <input name="name" type="text" required>
  <input name="email" type="email" required>
  <input type="submit">
</form>

每当我提交表单时,它都不会将数据写入JSON文件。我没有错误。有人可以帮我吗?

0 个答案:

没有答案