2016-05-18 183 views
0

當我用POSTMAN發送郵件請求時,我沒有任何問題,但是當我使用alamofire時,我遇到了問題。該帖子仍然通過alamofire請求,但數據收到的方式不同。什麼是一個alamofire請求看起來這是完全一樣的,如下郵遞員......郵遞員請求Alamofire請求

enter image description here

+0

你可以發佈一些代碼? –

回答

0

斯威夫特2.X

typealias apiSuccess = (result: NSDictionary?) -> Void 
typealias apiProgress = (result: NSDictionary?) -> Void // when you want to download or upload using Alamofire.. 
typealias apiFailure = (error: NSDictionary?) -> Void 

// Normal http request with JSON response.. 
func callJSONrequest(url:String, params:[String: AnyObject]?, success successBlock :apiSuccess, 
        failure failureBlock :apiFailure) { 

    Alamofire.request(.POST, url, parameters: params, encoding: ParameterEncoding.URL) 
     .responseJSON { response in 
      print("\(response.request?.URL)") // original URL request 
      //print(response.response) // URL response 
      //print(response.data)  // server data 
      //print(response.result) // result of response serialization 
      if response.result.isSuccess { 
       let jsonDic = response.result.value as! NSDictionary 
       successBlock(result: jsonDic) 

      } else { 
       let httpError: NSError = response.result.error! 
       let statusCode = httpError.code 
       let error:NSDictionary = ["error" : httpError,"statusCode" : statusCode] 
       failureBlock(error: error) 
      } 
    } 
} 

func myFunction() { 
    let myApiSuccess: apiSuccess = {(result: NSDictionary?) -> Void in 
     print ("Api Success : result is:\n \(result)") 
     // Here you can make whatever you want with result dictionary 
    } 

    let myApiFailure: apiFailure = {(error: NSDictionary?) -> Void in 
     print ("Api Failure : error is:\n \(error)") 
     // Here you can check the errors with error dictionary looking for http error type or http status code 
    } 
    var params :[String: AnyObject]? 
    let email : String! = "[email protected]" 
    let password : String! = "thisismypassword" 
    params = ["email" : email, "password" : password] 
    let url : String! = "https://arcane-brook-75067.herokuapp.com/login" 
    callJSONrequest(url, params: params, success: myApiSuccess, failure: myApiFailure) 
} 
+0

嗨,我正在努力使這項工作。底部功能出現錯誤。首先,爲什麼在函數的第一個括號之後有一個逗號...我還得到了'表達式解析爲在每個let myApiSuccess中使用未使用的函數,並讓myApiFailure – user1990406

+0

我現在檢查,可能在複製和粘貼時從我的個人回購..等待.. –

+0

好的,現在試試,有一個逗號和2個括號可以刪除.. –