简体   繁体   English

Mac OS X上的Node.js出现错误“ Access-Control-Allow-Origin不允许原始http:// localhost”

[英]Node.js on Mac OS X getting error “Origin http://localhost is not allowed by Access-Control-Allow-Origin”

I recently bought my first Apple Mac (which is running Mavericks) on which I have installed node.js (of which I am also a novice). 我最近购买了第一台Apple Mac(运行Mavericks),并在其上安装了node.js(我也是新手)。

I have run into an issue which hopefully someone will be able to provide me with a simple answer to. 我遇到了一个问题,希望有人能够为我提供一个简单的答案。 I am getting the error... 我遇到了错误...

Origin http://localhost is not allowed by Access-Control-Allow-Origin . Origin http://localhost is not allowed by Access-Control-Allow-Origin

I started the apache server, created a small html file called browser.html and placed it in /library/webserver/documents/ 我启动了apache服务器,创建了一个名为browser.html的小html文件,并将其放在/ library / webserver / documents /

This allows me to view it at http://localhost/browser.html 这使我可以在http://localhost/browser.html上查看它

This html file contains the following script… 该html文件包含以下脚本…

$.ajax('http://127.0.0.1:8124/littletest.js', {
    success: function() {console.log('browser success')},
    error: function() {console.log('browser fail')}
});

I then created a file called littletest.js in the same folder as browser.html (I previously had it in /users/[my mac's user name]/test/ but moving made no difference to the problem) 然后,我创建了一个名为littletest.js在同一文件夹中browser.html (我以前曾在/用户/ [我的Mac的用户名] /测试/移动,但做的问题没有区别)

The contents of the littletest.js file are… littletest.js文件的内容是…

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello Node.js\n');
}).listen(8124, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8124/');

By going to this folder in Terminal and entering node littletest.js the script starts. 通过在终端littletest.js到此文件夹并输入节点littletest.js ,脚本将启动。

But as mentioned above, when I browse to http://localhost/browser.html I get the error… 但是如上所述,当我浏览至http://localhost/browser.html ,出现了错误……

XMLHttpRequest cannot load http://127.0.0.1:8124/littletest.js. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

I know this can be resolved by adding the following lines to the littletest.js file… 我知道可以通过在littletest.js文件中添加以下几行来解决此问题……

res.setHeader('Access-Control-Allow-Origin', 'http://localhost');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');

…but surely it isn't normal to have to do this every time, is it?! …但是肯定每次都必须这样做是不正常的,对吗?

Is this normal? 这正常吗? (because it generally never mentions anything about this in any of the examples I've read). (因为它通常在我阅读的任何示例中都没有提及此事)。

Or is it that I am setting up my environment wrong? 还是我设置的环境错误? Please help! 请帮忙!

Thanks for any assistance you can give because this has been perplexing me for some time now! 感谢您提供的任何帮助,因为这已经困扰了我一段时间了!

You're using 127.0.0.1:8124 in one place and localhost in another. 您在一个地方使用127.0.0.1:8124在另一个地方使用localhost You and I know that those are owned by the same person, but they look different to the browser, so the cross-origin access policies are enforced. 您和我知道它们是同一个人拥有的,但是它们与浏览器的外观不同,因此将强制执行跨域访问策略。 If you just normalize on one of those (it shouldn't matter which you use, as long as you're consistent) the problem should be fixed. 如果您仅对其中之一进行标准化(只要您保持一致,那么使用哪个都无关紧要),则应该解决此问题。 Note that this means both using the same domain ( 127.0.0.1 or localhost ) as well as using the same port. 请注意,这意味着使用相同的域( 127.0.0.1localhost )以及使用相同的端口。

暂无
暂无

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

相关问题 Access-Control-Allow-Origin不允许来源http:// localhost:8080 - Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许使用origin http:// localhost:1716 - Origin http://localhost:1716 is not allowed by Access-Control-Allow-Origin XHR错误:原始http://本地主机不允许使用Access-Control-Allow-Origin - XHR Error : Origin http://localhost is not allowed by Access-Control-Allow-Origin 原始本地主机:Access-Control-Allow-Origin不允许 - Origin localhost: is not allowed by Access-Control-Allow-Origin Ajax-'Origin localhost不允许Access-Control-Allow-Origin' - Ajax - 'Origin localhost is not allowed by Access-Control-Allow-Origin' XMLHTTPRequest无法加载http:// ...原始http:// localhost:Access-Control-Allow-Origin不允许使用端口 - XMLHTTPRequest cannot load http://… Origin http://localhost:port is not allowed by Access-Control-Allow-Origin 错误:所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost:8000” - Error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access spine, node.js (express) 和 Access-Control-Allow-Origin - spine, node.js (express) and Access-Control-Allow-Origin XMLHttpRequest无法加载http:// localhost:8089 / jquery。 Access-Control-Allow-Origin不允许使用原点null - XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin Google的Places API和JQuery请求-Access-Control-Allow-Origin不允许来源http:// localhost - Google's Places API and JQuery request - Origin http://localhost is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM