2017-01-22 58 views
0

蔭嘗試使用Alamofire發送JSON請求,但它不工作的JSON請求這是我的JSON:Alamofire 4.3不能發送JSON請求

{"ClientID":"55050","AdminId":"myemail","Password":"123"} 

content-type → application/json; charset=utf-8 

,這裏是我的代碼:

import Alamofire 

func doLogin(username:String ,password:String) { 
    let parameters = ["ClientID":"55050" , "AdminId":username,"Password":password] 

    Alamofire.request("myurl.com", method: .post, parameters: parameters, encoding: JSONEncoding(options: [])).responseJSON(completionHandler: { 
     response in 
     print(response) 
    }) 

和這裏的,我正在逐漸

FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 3." UserInfo={NSDebugDescription=Invalid value around character 3.}))

+0

根據錯誤,問題是從你的服務器的響應。 – Larme

+0

我試了郵遞員,它的工作 – Muhammed

回答

0

通過讀取響應文檔我只需要把頭設置請求爲應用程序/ JSON

,所以我增加了額外的參數頭:

let headers: HTTPHeaders = [ 
      "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", 
      "Accept": "application/json" 
     ] 
Alamofire.request(ApiKeys().login, method : .post , parameters : parameters, encoding: JSONEncoding.default , headers: headers).responseJSON { response in 

     } 

,現在它的工作非常感謝大家的努力幫助:)

0

看來你必須在你的請求中遺漏標題。所以沒有迴應。 您的標題必須是字典[String: String]

let header = ["Content-Type" : "application/json"] 

而且你的樣品要求應該是:

Alamofire.request(getCategoryPath, method: .post, parameters: parameters, encoding: URLEncoding.default, headers: header) 
     .responseJSON { response in 
    guard response.result.error == nil else { 
      print(response.result.error!) 
} 

print(response.result.value) 

}