简体   繁体   English

这是我使用 javascript 和 Z2705A83A5A0659CCE34583972637EDA 发送 JSON object 的代码

[英]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 发送到服务器

  • input filed has many type (username, email,radio button, checkbox,textarea..)输入字段有多种类型(用户名、email、单选按钮、复选框、文本区域..)
  • in case of text input, it is run perfectly在文本输入的情况下,它运行完美
  • the problem occurs in case of radio buttons and checkbox, it doesnot send the data as a jason format any help please?在单选按钮和复选框的情况下会出现问题,它不会以 jason 格式发送数据有什么帮助吗? thanks in advance提前致谢

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:更多帮助:

  1. W3Schools W3学校
  2. MDN MDN
  3. StackOverflow 堆栈溢出

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

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