2016-04-15 94 views
0

POST請求,我想成爲一個類型的變量發送給詞典服務器,但不過就行了,我面臨的問題let task = session.dataTaskWithRequest(todosUrlRequest)錯誤:Cannot convert value of type 'NSURL' to expected argument type 'NSURLRequest'錯誤在迅速

我有兩個問題

1)這個錯誤是什麼?

2)是否有我用於POST的過程是正確的?不需要別的東西。 ?? 謝謝大家幫忙

func data_request(){ 

      let url = "http://sample.com/api/Flight/GetTicketInformation" 
      guard let todosUrlRequest = NSURL(string: url) else { 
       print("Error: cannot create URL") 
       return 
      } 

      let request = NSMutableURLRequest(URL: todosUrlRequest) 
      request.HTTPMethod = "POST" 
      request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData 

      let newTodo = ["Roundtrip": roundTrip, 
          "OneWay": oneWay, 
          "MultiWay": multiWay, 
          "Adult": numberAdults, 
          "Child": numberchild, 
          "Baby": numberinfant, 
          "SourceCityId": cityIDOrigin, 
          "DestinationCityId": cityIDPurpose, 
          "DepartingDate": raftDate, 
          "ReturningDate": bargashtDate ] 
      let jsonTodo: NSData 
      do { 
       jsonTodo = try NSJSONSerialization.dataWithJSONObject(newTodo, options: []) 
       request.HTTPBody = jsonTodo 

      } catch { 
       print("Error: cannot create JSON from todo") 
       return 
      } 
      request.HTTPBody = jsonTodo 

      let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
      let session = NSURLSession(configuration: config) 

      let task = session.dataTaskWithRequest(todosUrlRequest) { 
       (data, response, error) in 

       guard let responseData = data else { 
        print("Error: did not receive data") 
        return 
       } 
       guard error == nil else { 
        print("error calling POST on /todos/1") 
        print(error) 
        return 
       } 

       // parse the result as JSON, since that's what the API provides 
       do { 
        guard let receivedTodo = try NSJSONSerialization.JSONObjectWithData(responseData, 
                         options: []) as? [String: AnyObject] else { 
                          print("Could not get JSON from responseData as dictionary") 
                          return 
        } 
        print("The todo is: " + receivedTodo.description) 


       } catch { 
        print("error parsing response from POST on /todos") 
        return 
       } 
      } 
      task.resume() 
     } 

回答

2

request,而不是todosUrlRequest上線let task = session.dataTaskWithRequest(todosUrlRequest)

關於第二個問題,不知道。對不起

+0

感謝您的幫助 – Shadow