當我用POSTMAN發送郵件請求時,我沒有任何問題,但是當我使用alamofire時,我遇到了問題。該帖子仍然通過alamofire請求,但數據收到的方式不同。什麼是一個alamofire請求看起來這是完全一樣的,如下郵遞員......郵遞員請求Alamofire請求
0
A
回答
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個括號可以刪除.. –
相關問題
- 1. 郵遞員身體請求
- 2. curl的郵遞員請求
- 3. Alamofire JSON請求
- 4. Alamofire 3.0請求
- 5. Alamofire POST請求
- 6. 與郵遞員請求令牌
- 7. Alamofire多重請求
- 8. Alamofire請求緩慢?
- 9. Alamofire請求響應?
- 10. 調試Alamofire請求
- 11. 延遲Alamofire請求
- 12. 如何在另一個Alamofire請求中運行Alamofire請求?
- 13. 如何將此請求輸入到郵遞員請求的正文中?
- 14. PUT請求可與郵遞員,但不能與請求節點模塊
- 15. 如何使javax.ws的發佈請求類似於郵遞員請求?
- 16. 問題與Alamofire請求
- 17. 創建請求體Alamofire
- 18. Alamofire HTTP請求失敗
- 19. Alamofire POST請求的問題
- 20. 發佈方法請求Alamofire
- 21. Alamofire做兩個請求
- 22. Alamofire請求被取消
- 23. 經典郵寄請求vs ajax請求
- 24. 無法發送郵寄請求使用郵遞員與Django
- 25. req返回undefined〜使用郵遞員發送郵件請求
- 26. Alamofire請求總是失敗,如果Socket.io連接「請求超時」
- 27. 發送一個參數到郵遞員API獲取請求
- 28. 不能與郵遞員發送PUT請求
- 29. 從郵遞員請求中刪除出處標頭
- 30. 蔚藍的400錯誤的請求從郵遞員
你可以發佈一些代碼? –