简体   繁体   English

json-server 在 Chrome 中“访问本地主机被拒绝”

[英]json-server "Access to localhost was denied" in Chrome

I just started using json-server and created a script, 'npm run server' to build it.我刚开始使用 json-server 并创建了一个脚本“npm run server”来构建它。 However, when I try to access the JSON in my browser, I get "Access to localhost was denied".但是,当我尝试在我的浏览器中访问 JSON 时,出现“访问本地主机被拒绝”。

Here's the script:这是脚本:

json-server --watch db.json --port 5000

在此处输入图像描述

It's due to the AirPlay receiver in Mac OS Monterey using port 5000. More details at this answer .这是由于 Mac OS Monterey 中的 AirPlay 接收器使用端口 5000。此答案中有更多详细信息。

You can either disable AirPlay (System preferences --> Sharing), or start the server on another port in your package.json as mentioned:您可以禁用 AirPlay(系统首选项 --> 共享),或者在package.json中的另一个端口上启动服务器,如下所述:

--port 3000

I had the same issue with port 5000. I think it might be due to the port already being used by something else (if you're on a Mac, I believe it started with Monterey).我在端口 5000 上遇到了同样的问题。我认为这可能是由于该端口已被其他东西使用(如果您使用的是 Mac,我相信它是从 Monterey 开始的)。 I changed it to another port and it worked.我将其更改为另一个端口,并且可以正常工作。

Same, I just changed the port flag in the json script to 3000, re-ran the server and all good.同样,我只是将 json 脚本中的端口标志更改为 3000,重新运行服务器,一切正常。

 "scripts": {
    "server": "json-server --watch db.json --port 3000"
  }

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

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