[英]here is my code to send JSON object using javascript and ajax
so that sendJason function will run after press submit button to send data from inputs fields to server as a jason object这样 sendJason function 将在按下提交按钮后运行,以将输入字段中的数据作为 jason object 发送到服务器
javascript code: javascript 代码:
function sendJSON(){
let result = document.querySelector('.result');
let aed_modifing = document.querySelector('#aed_modifing');
let compliant_on_AED = document.querySelector('#compliant_on_AED');
let frequency_per_month = document.querySelector('#frequency_per_month');
let last_visit = document.querySelector('#last_visit');
let last_visit_seizure = document.querySelector('#last_visit_seizure');
let same_seizure_type = document.querySelector('#same_seizure_type');
let triggering_factors = document.querySelector('#triggering_factors');
// Creating a XHR object
let xhr = new XMLHttpRequest();
let url = "submit.txt";
// open a connection
xhr.open("POST", url, true);
// Set the request header i.e. which type of content you are sending
xhr.setRequestHeader("Content-Type", "application/json");
// Create a state change callback
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Print received data from server
result.innerHTML = this.responseText;
console.log(this.responseText);
}
};
// Converting JSON data to string
var data = JSON.stringify({ "aed_modifing": aed_modifing.value,
"compliant_on_AED":compliant_on_AED.value ,
"frequency_per_month" : frequency_per_month.value , "last_visit" : last_visit.value ,
"last_visit_seizure" :last_visit_seizure.value ,
"same_seizure_type":same_seizure_type.value,
"triggering_factors":triggering_factors.value
});
// Sending data with the request
xhr.send(data);
}
Remember that value of radio button and checkboxes is actually the checked
attribute which would be true
if it selected and false if not.请记住,单选按钮和复选框的值实际上是checked
的属性,如果选中则为true
,否则为 false。 You should aim to check and send that data instead of using value
.您应该旨在检查和发送该数据,而不是使用value
。
This also implies the check-boxes to send data, you need to process it in your javascript and similarly for radio-button to know which of them are actually checked.这也意味着发送数据的复选框,您需要在 javascript 中处理它,并且类似地,单选按钮可以知道其中哪些是实际检查的。
For radio button and checkboxes, the value
attribute is not very useful in determining which boxes are checked or not.对于单选按钮和复选框, value
属性在确定哪些框被选中或不被选中时不是很有用。
More help:更多帮助:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.