2017-01-22 121 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) 

}