简体   繁体   English

如何将HTML表单输入插入API对象数组

[英]How to insert HTML form input into API object array

I have two code "snippets" that I would like to combine but I don't really know how to. 我想合并两个代码“片段”,但我真的不知道该怎么做。

I've managed to make HTML form input into an JSON object. 我设法使HTML表单输入到JSON对象中。 See codepen here . 在此处查看codepen。

When I click the "Add note" btn I want to grab the input values -> make them to an object -> POST them to my api object array. 当我单击“添加注释” btn时,我想获取输入值->使它们成为对象->将它们发布到我的api对象数组中。 Image and code example below: 下面的图像和代码示例:

HTML to JSON HTML到JSON 在此处输入图片说明

// CREATE NEW NOTES
var btn = document.getElementById('btn');

btn.addEventListener("click", function() {

var request = new XMLHttpRequest();

request.open('POST', 'https://timesheet-1172.appspot.com/af25fa69/notes');

request.setRequestHeader('Content-Type', 'application/json');

request.onload = function () {

    // When I click the "Add note" btn I want to grab the input values -> make them to an object -> POST them to my api object array.

    // Like

    // var body = {
    //  'title': form.title.value,
    //  'description': form.description.value
    // }

};

request.send(JSON.stringify(body));

});

This is the current code that I have which let's me send an already set variable with object properties. 这是当前的代码,让我发送一个已设置的带有对象属性的变量。 See image below: 见下图:

SEND JSON 发送JSON 在此处输入图片说明

However my question is regarding how i can combine these tho examples so I can make dynamic inputs -> convert them to a JSON object -> Insert them in the api object array. 但是我的问题是关于如何结合这些示例,以便可以进行动态输入->将它们转换为JSON对象->将它们插入api对象数组。

Feel free to correct me if you think that i'm on the wrong path or if you think have a solution! 如果您认为我走错了路或认为有解决方案,请随时纠正我!

If you have any questions or need clarification don't hesitate to ask! 如果您有任何疑问或需要澄清,请随时提出!

Thanks beforehand! 预先感谢!

// E // E

You could combine them like this (and I think this is the best approach): 您可以这样组合它们(我认为这是最好的方法):

Define a function that will be executed on form submit, it will create JSON and send it via ajax: 定义一个将在表单提交时执行的函数,它将创建JSON并通过ajax发送:

function formatJSONAndSend(form) {
  var body = {
    title: form.title.value,
    description: form.description.value
  };

  var request = new XMLHttpRequest();
  request.open('POST', 'https://timesheet-1172.appspot.com/af25fa69/notes');
  request.setRequestHeader('Content-Type', 'application/json');    
  request.send(JSON.stringify(body));
}

And you can call it from your HTML like: 您可以通过HTML调用它,例如:

<form onsubmit="formatJSONAndSend(this);">
    Title: <input type="text" class="form-control" name="title" placeholder="Title goes here..." /><br /> Description: <input type="text" class="form-control" name="description" placeholder="Description goes here..." /><br />
    <input class="btn btn-primary" id="btn" type="submit" value="Add note">
</form>

After you submit your form by clicking on Add note button it will call the formatJSONAndSend() function which will first grab the data from the form and put it in body object which is then sent via POST to your web api. 在通过单击添加注释按钮提交表单之后,它将调用formatJSONAndSend()函数,该函数将首先从表单中获取数据并将其放入body对象中,然后通过POST发送到您的Web api。

Here's a working example: Codepen 这是一个工作示例: Codepen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM