Javascript 从Swift 4向Node.js发送post请求

Javascript 从Swift 4向Node.js发送post请求,javascript,ios,node.js,swift,express,Javascript,Ios,Node.js,Swift,Express,嗨,伙计们,我在发送值时遇到了问题​​通过Swift 4对node.js服务器的post请求(我在下面输入代码),以及node.js express的使用,在xcode上我看到以下错误:无法连接到服务器。我在localhost上使用node.js进行了测试,有人能帮我吗?谢谢 快捷功能: @IBAction func LoginFunction(_ sender: Any) { // prepare json data let json: [String: Any] = ["t

嗨,伙计们,我在发送值时遇到了问题​​通过Swift 4对node.js服务器的post请求(我在下面输入代码),以及node.js express的使用,在xcode上我看到以下错误:无法连接到服务器。我在localhost上使用node.js进行了测试,有人能帮我吗?谢谢

快捷功能:

@IBAction func LoginFunction(_ sender: Any) {

    // prepare json data
    let json: [String: Any] = ["title": "ABC",
                               "dict": ["1":"First", "2":"Second"]]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    // create post request
    let url = URL(string: "http://localhost:3000/login")!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    // insert json data to the request
    request.httpBody = jsonData

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        guard let data = data, error == nil else {
            print(error?.localizedDescription ?? "No data")
            return
        }
        let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
        if let responseJSON = responseJSON as? [String: Any] {
            print(responseJSON)
        }
    }

    task.resume()


}
Server Node.js

'use strict'

const express = require('express')
const bodyParser = require('body-parser')

// Create a new instance of express
const app = express()

// Tell express to use the body-parser middleware and to not parse extended bodies
app.use(bodyParser.urlencoded({ extended: false }))

// Route that receives a POST request to /sms
app.post('/login', function (req, res) {
  const body = req.body.Body
  res.set('Content-Type', 'text/plain')
  res.send(`You sent: ${body} to Express`)
})

// Tell our app to listen on port 3000
app.listen(3000, function (err) {
  if (err) {
    throw err
  }

  console.log('Server started on port 3000')
})

首先,这取决于您是在设备上运行还是在模拟器上运行。 如果您在设备上运行,则无法通过localhost访问服务器,因为localhost指向设备,因此您只能在模拟器上运行时使用localhost;在设备上运行时,应指定计算机的专用ip。 下面是一个快速shell方法,可以打印和复制您的ip:

ifconfig en0 | awk '/inet.[0-9]/ {print  "\033[1m \033[31m"$2 }' && ifconfig en0 | awk '/inet.[0-9]/ {printf $2}' | pbcopy -pboard general
只需将其粘贴到终端并按enter键

其次,这可能是因为您需要在应用程序中启用任意加载,在iOS 9及更高版本中,http调用在默认情况下被阻止 以下是如何做到这一点:


希望有帮助。

首先,这取决于您是在设备上运行还是在模拟器上运行。 如果您在设备上运行,则无法通过localhost访问服务器,因为localhost指向设备,因此您只能在模拟器上运行时使用localhost;在设备上运行时,应指定计算机的专用ip。 下面是一个快速shell方法,可以打印和复制您的ip:

ifconfig en0 | awk '/inet.[0-9]/ {print  "\033[1m \033[31m"$2 }' && ifconfig en0 | awk '/inet.[0-9]/ {printf $2}' | pbcopy -pboard general
只需将其粘贴到终端并按enter键

其次,这可能是因为您需要在应用程序中启用任意加载,在iOS 9及更高版本中,http调用在默认情况下被阻止 以下是如何做到这一点:


希望对您有所帮助。

您尝试过吗?@FrancescoDeliro是的,我尝试过,但不起作用。您是否在plist文件中设置了允许任意加载?您的服务器正在运行吗?。默认情况下,不允许使用非https通信,您需要在plist中设置此选项以允许使用httptraffic@Scriptable是的,我已经设置好了,我的服务器运行正常。你试过了吗?@FrancescoDeliro是的,我试过了,但不起作用。你有没有设置允许在plist文件中任意加载?您的服务器正在运行吗?。默认情况下,不允许使用非https通信,您需要在plist中设置此选项以允许使用httptraffic@Scriptable是的,我已经设置好了,我的服务器运行正常