简体   繁体   English

Localhost拒绝来自Javascript的连接

[英]Localhost refuses connection from Javascript

I am making a single web app that takes information from MYSQL database and displays it on screen using AngularJS and NodeJS. 我正在制作一个Web应用程序,该应用程序从MYSQL数据库获取信息,并使用AngularJS和NodeJS在屏幕上显示该信息。 I have set up a node.js server that gives out JSON file from a database. 我已经设置了一个node.js服务器,该服务器从数据库发出JSON文件。 It works fine if I connect to it like this: 192.168.1.57:8888 which is my home server ip or remote connecting my server and using localhost:8888. 如果我像这样连接到它,则可以正常工作:192.168.1.57:8888这是我的家庭服务器IP或远程连接我的服务器并使用localhost:8888。 Doing this downloads the JSON file on my computer. 这样做会将JSON文件下载到我的计算机上。

However. 然而。 If I'm using javascript to get JSON file from the server it gives me this error: 如果我使用JavaScript从服务器获取JSON文件,则会出现此错误:

GET http://localhost:8888/ net::ERR_CONNECTION_REFUSED

I have tried connecting to the server with both AngularJS and JQuery and they both give the same error. 我尝试使用AngularJS和JQuery连接到服务器,并且它们都给出相同的错误。 I've also tried 127.0.0.1 and that doesn't work either. 我也尝试了127.0.0.1,但这也不起作用。 What can I do to fix it or can I do this with a better alternative way? 我该怎么做才能解决此问题,或者可以采用更好的替代方法来做到这一点?

This is the server code in node.js 这是node.js中的服务器代码

var http = require("http");
mysql = require("mysql");

var connection = mysql.createConnection({
user: "test",
password: "test",
database: "test"

});


http.createServer(function (request, response) { 

request.on('end', function () { 

    connection.query('SELECT * FROM feedback;', function (error, rows, fields) { 
        response.writeHead(200, { 
            'Content-Type': 'x-application/json' 
        }); 

        response.end(JSON.stringify(rows)); 
    }); 
}); 


}).listen(8888);

this is the client side in angularJS: 这是angularJS的客户端:

(function(){
var app = angular.module('question', [ ]);

app.controller("ServerController", [ '$http', function($http){
    $http.get("http://localhost:8888").success(function(data,status,headers,config){
        console.log("success");
    }).error(function(data,status,headers,config){
        console.log("error");
    });

} ]);
}
)();

Taking a guess at it, you're running into the Same Origin Policy , which requires that the page making the ajax request be in the same origin as the resource it's requesting (or that the server serving the resource supports CORS and allows the page's origin). 猜一猜,您遇到了Same Origin Policy ,该策略要求发出ajax请求的页面与它所请求的资源具有相同的起源(或者服务该资源的服务器支持CORS并允许页面的起源)。

Two different ways this might be hitting you (I can't tell which, the question doesn't give enough information): 这可能会打到您的两种不同方式(我无法告诉您,问题没有提供足够的信息):

  • If the browser code is running in a page loaded from the file system, not via HTTP, then it's a cross-origin request because the protocols don't match ( file: vs. http: ). 如果浏览器代码运行在从文件系统加载的页面中,而不是通过HTTP运行,那么这是跨域请求,因为协议不匹配( file: vs. http:

  • If the page has not been loaded from localhost:8888 , but instead from some other port on localhost , then the origins don't match because the port numbers are different. 如果页面尚未从加载localhost:8888 ,而是从其他一些端口localhost ,然后来历不匹配,因为端口号是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM