2017-11-16 1 views
0

인사말. 서버 코드에 여러 데이터를 전달하려고했습니다. 내 프로그램의 아래 흐름을 찾으십시오. HS110 :AJAX에서 여러 데이터 전달

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); 

를 어떻게 서버에 객체 데이터 ID2를 전달하는 방법과 별도의 데이터로 그것을 처리하는 방법 좀 도와 주 시겠어요? 미리 감사드립니다.

답변

0
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, 

}); 
}); 
} 
+0

감사합니다. Jeevanswamy –

관련 문제