当我向服务器发送数据时,Post完全正常并且完整有效但错误
erro return:
B
Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
完美地从服务器返回数据
data =
[{ “$ id”:“1”, “Id”:14, “来源”:23, “Destination_”:21, “独家”:虚假, “AdultPrice”:“70766”, “ChildPrice”:“77076”, “BabyPrice”:“54109”, “VetranPrice”:“118664”, “DepartureDate”:“1997-09-25T00:00:00”, “ArrivalDate”:“2001-03-24T00:00:00”, “TrainTypeId”:1, “NumberofSeats”:153}]
你可以说出原因是什么?
let request = NSMutableURLRequest(URL: todosUrlRequest)
request.HTTPMethod = "POST"
request.setValue("application/json; charset=uft-8", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
//3
let newTodo = [
"NumberOfPassenger": tedat,
"Source": cityIDSource,
"Destination": cityIDDestination,
"DapartureDate": raftDate,
"TicketType": typeTrainID,
"TravelType": 6,
"CloseDoor": copeqtar
]
print(newTodo)
//4
let jsonTodo: NSData
do {
jsonTodo = try NSJSONSerialization.dataWithJSONObject(newTodo, options: [])
request.HTTPBody = jsonTodo
} catch {
print("Error: cannot create JSON from todo")
return
}
print(jsonTodo)
request.HTTPBody = jsonTodo
//5
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
let task = session.dataTaskWithRequest(request) {
(data, response, error) in
// statusResponse
if let HTTPResponse = response as? NSHTTPURLResponse {
let statusCode = HTTPResponse.statusCode
if statusCode == 200 {
print(response)
//6
print(data)
if data != nil {
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
}
guard let responseData = data else {
return
}
self.dataRecive = responseData
print("currect")
}else{
print("error calling POST on /todos/1")
print(error)
}
}
dispatch_async(dispatch_get_main_queue()){
actInd.stopAnimating()
}
}
task.resume()