【问题标题】:Passing multiple data in AJAX在 AJAX 中传递多个数据
【发布时间】:2017-11-16 14:36:23
【问题描述】:

您好。我想将多个数据传递给我的服务器代码。请找到我的程序的以下流程。 HS110:

function turnplug2On(){

var newval=this.getAttribute("data-id");
var newval1=this.getAttribute("data-id2");
newval=newval+"On"
console.log(newval);

$(document).ready(function() {   
 //console.log(newval);
 $.ajax({
        type: "POST",
        url: 'http://localhost:8124/',
        timeout: 2000,  
        data: JSON.stringify({data:newval}),        

        //data: '{"data":"plug2On"}',
        //data:newval,

    });
});
}
$(document).ready(function () {
Array.from(document.getElementsByClassName("turn-on")).forEach(function      (button) {
button.onclick = turnplug2On;
});
});

ajax.js:

http.createServer(function (req, res) {
//console.log('request received');
//util.log(util.inspect(req));
console.log(req,res);

res.writeHead(200, {'Content-Type': 'text/plain'});
req.on('data', function (chunk) {
    console.log(chunk.toString());
    //req.on(function (chunk) {
    var obj = JSON.parse(chunk.toString());
    console.log(obj.data);

您能帮我如何将对象 data-id2 传递给服务器以及如何将其作为单独的数据处理吗? 提前致谢

【问题讨论】:

    标签: node.js ajax


    【解决方案1】:
    var newval=this.getAttribute("data-id");
    var newval1=this.getAttribute("data-id2");
     newval=newval+"On"
      console.log(newval);
    
      $(document).ready(function() {   
     //console.log(newval);
      $.ajax({
        type: "POST",
        url: 'http://localhost:8124/',
        timeout: 2000,  
        data: JSON.stringify({newval:newval,newval1:newval1}),        
    
        //data: '{"data":"plug2On"}',
        //data:newval,
    
    });
    });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多