[英]Sending string from Node.js (Express) server to iOS Swift 3
I am trying to create a login system for my iOS mobile application. 我正在尝试为我的iOS移动应用程序创建一个登录系统。 I have a request sending to my Node.js server with Swift 3 with: 我有一个请求使用Swift 3发送到我的Node.js服务器:
@IBAction func loginBtn(_ sender: UIButton) {
//created NSURL
let requestURL = NSURL(string: loginURL)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "POST"
//getting values from text fields
let empNum = empTextField.text
let empPass = passTextField.text
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "empNum=" + empNum! + "&empPass=" + empPass!;
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
print("myjson : \(String(describing: myJSON))")
//parsing the json
if let parseJSON = myJSON {
//creating a string
var msg : String!
//getting the json response
msg = parseJSON["message"] as! String?
//printing the response
print("message here: \(msg)")
}
} catch {
print("Error here: \(error)")
}
}
//executing the task
task.resume()
}
I am getting a 200 server side but I want to be able to res.send
a string
back to Swift so I can proceed to take further actions. 我得到一个200服务器端,但我希望能够res.send
一个string
回斯威夫特这样我就可以继续采取进一步的行动。 How do I check that response? 如何检查该回复? Like: 喜欢:
if response == "vaild" {
...
}else{
...
}
When I run this code I get an error that reads: 当我运行此代码时,我收到一条错误消息:
Error here: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
PS I am also not really familiar with the request code so if it is right in front of my face please just explain the code to me then. PS:我也不是很熟悉请求代码,所以如果它正好摆在我面前,那么请向我解释代码。 Sorry in advance!! 不好意思!
Your code sample suggests that your client expects a json response with a property message
in it. 您的代码示例建议您的客户端期望其中包含属性message
的json响应。 If you are using resp.send('Success')
from your server it will not be a json object. 如果您从服务器使用resp.send('Success')
,则它将不是json对象。 It will be a string. 这将是一个字符串。
If that is what you want on your server response you should update your client to simply parse the data to a string. 如果这是您希望在服务器响应上显示的内容,则应更新客户端以将数据简单地解析为字符串。 You can use String(data: data, encoding: .utf8)
where "data" is what is returned from the response. 您可以使用String(data: data, encoding: .utf8)
,其中“ data”是响应中返回的内容。
However, I would recommend leveraging HTTP Status codes. 但是,我建议您利用HTTP状态代码。 In your server code you can respond with a 422 if login was not successful. 如果登录失败,则可以在服务器代码中以422进行响应。 This can be accomplished with resp.status(422).send('Some optional message')
. 这可以通过resp.status(422).send('Some optional message')
。 Then client side all you need to do is check the response status. 然后客户端需要做的就是检查响应状态。 Instead of a string compare. 而不是字符串比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.