Javascript GET/POST node.js(跨域)

Javascript GET/POST node.js(跨域),javascript,node.js,post,get,cross-domain,Javascript,Node.js,Post,Get,Cross Domain,我们的服务器已经可以从客户端接收字符串 我们希望客户端返回一个响应并在文本区域中显示它 app.js: var sys = require ('util'), url = require('url'), http = require('http'), qs = require('querystring'); var stringforfirefox = 'hi man!'; http.createServer(function (req, res) {

我们的服务器已经可以从客户端接收字符串

我们希望客户端返回一个响应并在文本区域中显示它

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");
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");
对于测试,我们使用本地主机url。稍后它将是跨域的。 这是网站

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

document.onkeypress=功能键按下(e){
如果(e.keyCode==112){
httpGet('http://localhost:1337/index77?title=message_for_server')  ;
}
如果(e.keyCode==113){
var-xmlhttp;
xmlhttp=新的XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“textarea1”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”http://localhost:1337/index77“,对);
send(“fname=Henry&lname=Ford”);
}
}
函数httpGet(theUrl)
{
var xmlHttp=null;
xmlHttp=新的XMLHttpRequest();
open(“GET”,theUrl,false);
send(“fname=Henry&lname=Ford”);
警报(xmlHttp.responseText);
}


我们想扩展这里的代码

您需要发送响应(res)

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

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


像Google News RSS提要这样的JavaScript工具使用这种方法来绕过这些安全壁垒。

在这里,客户端和服务器之间的字符串交换非常有效:

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



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

功能交换(数据){
document.getElementById('textarea1')。value=data;}
document.onkeypress=功能键按下(e){
如果(e.keyCode==112){
var关键字=document.getElementById('edit1')。值;
var script=document.createElement('script');
script.src=http://localhost:1337/?title=“+关键字;
document.body.appendChild(脚本);//触发GET请求
}
}



(来自我们另一方的代码组合。)

出现以下错误:TypeError:Object#没有方法“send”@MichaelMoeller
res.statusCode=200;res.end(“请求主体:“+主体”)(参考:,)
res.send()
可从其他库获得,例如。在这一点上,您可能会发现(来自)有用。@JonathanLonowski感谢您的支持。我们使用了这段代码,但客户端尚未显示任何反应。@MichaelMoeller需要检查几件事:1)确保为每个请求调用
res.end()
——包括
GET
POST
。2) Ajax通常不受
文件的支持://
,因此请确保该页面是通过HTTP/HTTPS提供的。3) 跨源Ajax需要额外的工作来验证权限,否则浏览器将拒绝请求。请参阅MDN的选项。感谢你们的帮助,伙计们,我们还没有让它工作(可能是因为我们这边的一个bug)。我们提出了另一个有趣的解决方案,并将其作为答案发布。