我试图命中alamofire后请求,但,获取请求失败,错误responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error:Error Domain=NSCocoaErrorDomain Code=3840“Invalid value around line 1,column 0.”UserInfo={NSDebugDescription=Invalid value around line 1,column 0.,NSJSONSerializationErrorIndex=0}))。请帮助我解决问题。谢谢。
下面是我的API处理程序方法
func apiPostRequest1(parameters:[String:String], url:String, completionHandler: @escaping (Any?) -> Swift.Void) {
var headers = HTTPHeaders(parameters)
headers.add(name: "Content-Type", value: "application/x-www-form-urlencoded; charset=UTF-8")
session.request(url,
method: .post,
parameters: parameters,
encoding: URLEncoding.httpBody,
headers: headers).validate(statusCode: 200..<600).responseJSON{ response in
switch response.result {
case .success(let JSON):
completionHandler(JSON)
case .failure(let error):
print("Request failed with error \(error)")
completionHandler(response.response?.statusCode)
}
}
}
2条答案
按热度按时间bakd9h0s1#
下面的代码是工作时,我从请求中删除头。
r1zhe5dt2#
就我而言,这是一个传递密码参数的问题。我正在传递编码的密码**requestDict[“password”] =(parameters?[“password”] as?String)?.encodeToBase64()我与后端团队进行了检查,我避免了这种情况,并传递了相同的文本密码requestDict[“password”] =(parameters?[“password”]**请确保您根据API要求传递正确的参数和正确的URL。