Google 서버가 이미 클라이언트에서 문자열을받을 수 있습니다.GET/POST node.js (교차 도메인)
고객에게 응답을 받고 텍스트 영역에 표시하고 싶습니다.
app.js :
var sys = require ('util'),
url = require('url'),
http = require('http'),
qs = require('querystring');
var stringforfirefox = 'hi man!';
http.createServer(function (req, res) {
if(req.method=='POST') {
var body='';
req.on('data', function (data) {
body +=data;
});
req.on('end',function(){
var POST = qs.parse(body);
console.log(POST);
});
}
else if(req.method=='GET') {
var url_parts = url.parse(req.url,true);
console.log(url_parts.query);
}
}).listen(1337, "127.0.0.1");
테스트를 위해 우리는 로컬 호스트 URL을 사용합니다. 나중에 교차 도메인이됩니다. 우리는 우리가 여기있는이 코드를 확장하고 싶습니다
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script>
document.onkeypress = function keypressed(e){
if (e.keyCode == 112) {
httpGet('http://localhost:1337/index77?title=message_for_server') ;
}
if (e.keyCode == 113) {
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("textarea1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","http://localhost:1337/index77",true);
xmlhttp.send("fname=Henry&lname=Ford");
}
}
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send("fname=Henry&lname=Ford");
alert(xmlHttp.responseText);
}
</script>
</head>
<body>
<form>
<p>
<textarea id="textarea1" cols="25" rows="25" name="textfeld"></textarea>
</p>
</form>
</body>
</html>
: 다음은 웹 사이트
index.html을합니다.
다음 오류가 발생합니다. _TypeError : Object #가 충족되지 않았습니다. hod 'send'_ –
@MichaelMoeller'res.statusCode = 200; res.end ("요청의 본문 :"+ body "); (ref : ['res.write()'] (http://nodejs.org/api/http.html#http_response_write_chunk_encoding), ['res.end()'(http://nodejs.org/api/http.html#http_response_end_data_encoding))'res.send()'는 추가 라이브러리에서 사용할 수 있습니다. [Express] (http://expressjs.com/api.html#res.send). 이 노트에서 [연결] (http://www.senchalabs.org/connect/)의 [bodyParser()'] (http://expressjs.com/api.html#bodyParser) 유능한. –
@JonathanLonowski님께 도움을 주셔서 감사합니다. 우리는이 코드를 사용했지만 클라이언트는 아직 어떤 반응도 보이지 않습니다. –