简体   繁体   English

将发布请求转换为 JSON 文件

[英]Transform a post request to a JSON file

I have a questionnaire on javascript(post request) and need to acquire the user's answers and then transform them into a JSON file, so far, I've tried this but I have no idea how to proceed, my code is down below.我有一份关于javascript(post request)的问卷,需要获取用户的答案,然后将它们转换为JSON文件,到目前为止,我已经尝试过了,但我不知道如何继续,我的代码在下面。

exports.answers= (req, res) =>{
    async function getISS(){
        const response = await fetch(req);
        const data =await response.json();
        console.log(data);
    }
    getISS();
    console.log(req.text);
    let resultado =[data];
    res.send(resultado);
};

The answers come from a survey like this this is the survey答案来自这样的调查这是调查

There are a number of ways to send the form data to the server.有多种方法可以将表单数据发送到服务器。

 const form = document.getElementById('my-form'), url = 'https://reqres.in/api/users'; form.onsubmit = submit; function submit(e) { e.preventDefault(); const formData = new FormData(form), data = {}; // Get a JSON object from the form data formData.forEach((value, key) => { data[key] = value; }); // Using fetch and await/async postData(url, data).then(res => { console.log('Fetch await/async response is: ' + JSON.stringify(res)); // JSON data parsed by `response.json()` call }); // Using fetch / then fetch(url, { method: 'POST', body: JSON.stringify(data), headers: { 'Content-Type': 'application/json' // 'Content-Type': 'application/x-www-form-urlencoded', }, }).then( res => res.json() ).then(json => console.log('Fetch / then response is: ' + JSON.stringify(json))); // Using XMLHttpRequest const req = new XMLHttpRequest(); req.open('POST', url, true); req.setRequestHeader('Content-type', 'application/json'); req.onreadystatechange = function() { if (this.readyState === XMLHttpRequest.DONE && this.status === 201) { console.log('XMLHttpRequest response is: ' + req.response); } } req.send(JSON.stringify(data)); } /* * This is the example function on the MDN site for fetch * https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch */ async function postData(url = '', data = {}) { // Default options are marked with * const response = await fetch(url, { method: 'POST', // *GET, POST, PUT, DELETE, etc. mode: 'cors', // no-cors, *cors, same-origin cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached credentials: 'same-origin', // include, *same-origin, omit headers: { 'Content-Type': 'application/json' // 'Content-Type': 'application/x-www-form-urlencoded', }, redirect: 'follow', // manual, *follow, error referrerPolicy: 'no-referrer', // no-referrer, *client body: JSON.stringify(data) // body data type must match "Content-Type" header }); return await response.json(); // parses JSON response into native JavaScript objects }
 <form action="survey" id="my-form"> <label for="name">Name</label> <input type="text" name="name" id="name"> <label for="job">Job</label> <input type="text" name="job" id="job"> <button>Submit</button> </form>

Any of the above methods should work to send your form data to the server as JSON.上述任何方法都可以将表单数据作为 JSON 发送到服务器。 I have used an online API to demonstrate.我使用了一个在线的API来演示。

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

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