在AJAX中传递多个数据 [英] Passing multiple data in AJAX

查看:49
本文介绍了在AJAX中传递多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候.我想将多个数据传递到服务器代码.请找到我程序的以下流程.HS110:

Greetings. I wanted to pass multiple data to my server code. Please find the below flow of my program. 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传递给服务器,以及如何将其作为单独的数据进行处理?预先感谢

Could you please help me how to pass the object data-id2 to server and how to handle that as a separate data ? Thanks in advance

推荐答案

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,

});
});
}

这篇关于在AJAX中传递多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆