ios - 为什么我无法从 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,我会收到第二个错误
解决方案
在这种情况下,您必须使用您的 IP 才能访问您的本地主机
前任:let url = URL(string: "http://192.168.1.105/signup")!
因此,您必须设置您拥有本地服务器的计算机的 IP,而不是使用 (localhost.com/...)。
推荐阅读
- visual-studio-code - vscode 的这个主题叫什么名字?
- sql - 将 Oracle 特定的 sql 迁移到 postgresql
- google-api - Google Drive API 不允许发布文件
- reactjs - 使用相同组件的两个组件
- php - 查询使用聚合时 Laravel mongodb 返回日期格式错误
- c# - PostAsync API 提供了错误的网关
- python - 导入 xgboost 模块时出现问题(错误消息:['[WinError 193] %1 is not a valid Win32 application'])
- python - 如何从行到列打印某些特定字符?
- python - 将动态加载的函数提交给 ProcessPoolExecutor
- c# - 确定已安装的 ASP.NET Core Hosting Bundle 版本