客户端:swift,xcode 8.3
server:nodejs,express,mongodb
在客户端,它首先向服务器发送登录请求(用户名和密码),收到有效令牌后,进入下一个屏幕发送另一个请求以获取书籍列表。
奇怪的是,对于书籍的第二个请求,有时它是有效的 - 如果使用调试模式和一些断点继续“缓慢”,而有时它不起作用 - 如果继续断点不是那么慢或没有他们。我重复了很多次并想出了这种模式。
以下是发送请求的代码段 - 数百万谢谢!!!
let url = URL(string: "http://" + SERVER_IP + ":" + PORT + "/books")
print("Query:>> url: ")
print(url)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
print("Query>> response from loadBooks")
if let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) {
var books = [Book]()
if let array = json as? [Any] {
for i in 0...array.count-1 {
var bookJson = array[i] as? [String: Any]
let b = Book(json: bookJson!)
books.append(b!)
}
}
print ("Query>> \(books.count)" + " books loaded, callback completion")
completion(books)
}
}
task.resume()
请求未成功时的控制台输出:
查询产品:>> url:可选(localhost:3001 / books)
2017-09-07 15:31:45.173596 + 0800 SimplyRead [76273:2379182] [] nw_endpoint_flow_service_writes [1.1 :: 1.3001 ready socket-flow(satisf)]写请求有4294967295帧计数,0字节计数查询>&gt ;来自loadBooks的回复
2017-09-07 15:51:41.210732 + 0800 SimplyRead [76273:2390856] [] tcp_connection_write_eof_block_invoke写入关闭回调收到错误:[89]操作已取消