首页 > 解决方案 > 为什么我无法从 iOs 模拟器访问我的 nodejs 本地服务器

问题描述

我正在尝试从我的 xcode 向在 nodejs 上运行的本地服务器发出 http 请求。以下是我的 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()

我收到以下回复

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

如果我使用 localhost.com,我会收到第一个错误,如果我将请求更改为 www.localhost.com,我会收到第二个错误

标签: iosnode.jshttp

解决方案


在这种情况下,您必须使用您的 IP 才能访问您的本地主机

前任:let url = URL(string: "http://192.168.1.105/signup")!

因此,您必须设置您拥有本地服务器的计算机的 IP,而不是使用 (localhost.com/...)。


推荐阅读