简体   繁体   English

为什么我无法从 iOs 模拟器访问我的 nodejs 本地服务器

[英]Why I can't access my nodejs local server from iOs simulator

I am trying to make an http request from my xcode to my local server running on nodejs.我正在尝试从我的 xcode 向在 nodejs 上运行的本地服务器发出 http 请求。 The following in a part of my iOs code.以下是我的 iOs 代码的一部分。

  let url = URL(string: "http://localhost.com/signup")!

    let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
        guard let data = data else { return }
        print(String(data: data, encoding: .utf8)!)
    }

    task.resume()

I recieve the following the response我收到以下回复

Error Domain=NSURLErrorDomain Code=-1001 "The request timed out."
Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be 
found.

I receive the first error if I use localhost.com and I receive the second error if I change the request to www.localhost.com如果我使用 localhost.com,我会收到第一个错误,如果我将请求更改为 www.localhost.com,我会收到第二个错误

in this case you have to use your IP to get access to your localhost在这种情况下,您必须使用您的 IP 来访问您的本地主机

ex: let url = URL(string: "http://192.168.1.105/signup")!例如: let url = URL(string: "http://192.168.1.105/signup")!

so Instead of using (localhost.com/...) you have to set the IP of your computer where you have LOCAL SERVER.所以不是使用 (localhost.com/...) 你必须设置你有本地服务器的计算机的 IP。

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

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