Javascript 连接到同一AWS实例不同端口上的node.js服务器,连接被拒绝

Javascript 连接到同一AWS实例不同端口上的node.js服务器,连接被拒绝,javascript,php,ajax,node.js,amazon-web-services,Javascript,Php,Ajax,Node.js,Amazon Web Services,我正在尝试连接到与web服务器位于同一台机器上的node.js服务器,该机器位于AWS Amazon linux实例上。当我尝试使用Ajax在127.0.0.1:8003或localhost:8003与服务器通信时,Firefox会出现“跨源请求被阻止”错误,Chrome会出现“net::ERR_CONNECTION_densed”错误 如果我直接从浏览器(即http:[服务器]:8003)访问机器,我会得到预期的响应。我还从机器上获得了curl localhost:8003的预期响应。我只看到

我正在尝试连接到与web服务器位于同一台机器上的node.js服务器,该机器位于AWS Amazon linux实例上。当我尝试使用Ajax在127.0.0.1:8003或localhost:8003与服务器通信时,Firefox会出现“跨源请求被阻止”错误,Chrome会出现“net::ERR_CONNECTION_densed”错误

如果我直接从浏览器(即http:[服务器]:8003)访问机器,我会得到预期的响应。我还从机器上获得了curl localhost:8003的预期响应。我只看到ajax连接的问题

现在node.js服务器很简单:

var sys = require('sys');
var http = require('http');

var server = http.createServer(
    function( request, response ) {

        var origin = "*"; // (request.headers.origin || "*");

        if (request.method.toUpperCase() === "OPTIONS"){
            // Echo back the Origin (calling domain) so that the
            // client is granted access to make subsequent requests
            // to the API.
            response.writeHead(
                "204",
                "No Content",
                {
                    "access-control-allow-origin": origin,
                    "access-control-allow-methods": "GET, POST, OPTIONS",
                    "access-control-allow-headers": "content-type, accept",
                    "access-control-max-age": 10, // Seconds.
                    "content-length": 0
                }
            );

            // End the response - we're not sending back any content.
            return( response.end() );

        }
        responseBody = "Hello world! from X AWS - version 2\n";
        response.writeHead(
            "200",
            "OK",
            {
                "access-control-allow-origin": origin,
                "content-type": "text/plain",
                "content-length": responseBody.length
            }
        );

        response.write( responseBody );
        response.end();
    }
);

server.listen(8003);
console.log("Server running on 8003...");
这里有一些额外的代码,试图查看问题是否出在访问控制允许源标题上

客户端页面也很简单:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>X BC API Tester</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>


    <script type="text/javascript">
    function call_bc_proxy()
    {
        console.log("Calling bc_proxy...");
        $.ajax({
            type: "GET",
            url: "http://localhost:8003",
            dataType: "text",
            success: function( response ){
                $( "#cms_response" ).html( response );
            },
            error: function( error ){

                // Log any error.
                console.log( "ERROR:", error );

            },
        });

        return false;
    }
    </script>

</head>
<body>
    <p>Test new BCOV API using oAuth2</p>
    <p><a href="#" onclick="return call_bc_proxy();" class="button">Get token</a></p>
    <div id="cms_response"></div>
</body>
</html>

X BC API测试仪
函数调用_bc_proxy()
{
log(“调用bc_代理…”);
$.ajax({
键入:“获取”,
url:“http://localhost:8003",
数据类型:“文本”,
成功:功能(响应){
$(“#cms_response”).html(response);
},
错误:函数(错误){
//记录任何错误。
日志(“错误:”,错误);
},
});
返回false;
}
使用oAuth2测试新的BCOVAPI

我想我可以通过Ajax调用PHP函数,并在PHP函数中使用curl来解决这个问题,但我想让它直接使用Javascript

任何想法都值得赞赏。不确定还有哪些信息有用


jd

对ajax脚本使用与浏览器调用中相同的url。当您调用url:“”时,您尝试调用的是本地计算机而不是aws

嗯。javascript正在浏览器的机器上运行。我不得不向负载平衡器添加另一个侦听器,但您的回答解决了这个问题。多谢!我也遇到了同样的问题,但这个解决方案对我不起作用。有什么建议吗?