swift Alamofire post request getting**第1行第0列附近的值无效,**

x759pob2  于 2023-03-28  发布在  Swift
关注(0)|答案(2)|浏览(192)

我试图命中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)
        }
    }
}
bakd9h0s

bakd9h0s1#

下面的代码是工作时,我从请求中删除头。

func apiPostRequest1(parameters:[String:String], url:String,  completionHandler: @escaping (Any?) -> Swift.Void) {
    
    session.request(url,
               method: .post,
               parameters: parameters,
               encoding: URLEncoding.httpBody).validate(statusCode: 200..<600).responseJSON{ response in
        switch response.result {
        case .success(let JSON):
            completionHandler(JSON)
        case .failure(let error):
            let responseData = String(data: response.data!, encoding: String.Encoding.utf8)
            print(responseData ?? "Error in encoding response data")
            print("Request failed with error \(error)")
            completionHandler(response.response?.statusCode)
        }
    }
}
r1zhe5dt

r1zhe5dt2#

就我而言,这是一个传递密码参数的问题。我正在传递编码的密码**requestDict[“password”] =(parameters?[“password”] as?String)?.encodeToBase64()我与后端团队进行了检查,我避免了这种情况,并传递了相同的文本密码requestDict[“password”] =(parameters?[“password”]**请确保您根据API要求传递正确的参数和正确的URL。

相关问题