簡體   English   中英

為什么我無法從 iOs 模擬器訪問我的 nodejs 本地服務器

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

我正在嘗試從我的 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")!

所以不是使用 (localhost.com/...) 你必須設置你有本地服務器的計算機的 IP。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM