2013-03-13 52 views
1

我们的服务器已经可以从客户端接收字符串。GET/POST node.js(跨域)

我们希望客户得到回复并将其显示在textarea中。

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

为了进行测试,我们使用本地主机地址。之后它将是跨域的。 这里是网站

的index.html:

<!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> 

我们想延长这种代码,我们在这里。

回答

0

您需要发送回复(res)。

if(req.method=='POST') { 
    var body=''; 
    req.on('data', function (data) { 
     body +=data; 
    }); 
    req.on('end',function(){ 
     res.send(200, "The request's body: " + body); 
     var POST = qs.parse(body); 
     console.log(POST); 
    }); 
+0

发生以下错误:_TypeError:Object# has no met hod'send'_ – 2013-03-14 00:08:40

+0

@MichaelMoeller'res.statusCode = 200; res.end(“request's body:”+ 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()'可从其他库中获得,例如[快递](http://expressjs.com/api.html#res.send)。在该说明中,您可以从[Connect](http://www.senchalabs.org/connect/)中找到['bodyParser()'](http://expressjs.com/api.html#bodyParser)有用。 – 2013-03-14 00:20:18

+0

@JonathanLonowski感谢您的支持。我们使用这个代码,但客户端没有显示任何反应。 – 2013-03-14 00:42:57

0

XMLHttpRequest()一个明显的安全原因不能用于发布或获取信息的交叉domainly。在某些浏览器中,它可能在您处于本地主机环境中时工作,但是当该网站从Web启动时,这是不可接受的。

要解决此问题,您必须将AJAX请求提交到同一个域,并在服务器端处理跨域操作。

Google News RSS Feed等JavaScript工具使用此方法来解决这些安全障碍。

0

这里的客户端和服务器之间串交换完美的作品:

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=='GET') { 

     res.statusCode = 200; 


     var url_parts = url.parse(req.url,true); 
     var query = url_parts.query["title"]; 
     console.log(query); 

     stringforfirefox = 'your input: '+query; 

     res.end("__stringexchange(" + JSON.stringify(stringforfirefox) + ");"); 
    } 


}).listen(1337, "127.0.0.1"); 

的index.html:

<!DOCTYPE html> 
<html> 
<head> 

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script> 
    <script> 


     function __stringexchange(data) { 

      document.getElementById('textarea1').value= data;} 


     document.onkeypress = function keypressed(e){ 

      if (e.keyCode == 112) { 
       var keyword = document.getElementById('edit1').value ; 
       var script = document.createElement('script'); 
       script.src = 'http://localhost:1337/?title='+keyword; 
       document.body.appendChild(script); // triggers a GET request 
      } 

     } 



    </script> 



</head> 
<body> 

<form> 
    <input id="edit1" type="text" name="keyword"><br> 
    <br> 
    <textarea id="textarea1" cols="25" rows="25" name="textfeld"></textarea> 

</form> 
</body> 
</html> 

(代码来自我们其它question组合)