我正在尝试使用一些参数进行发布请求,但是在发出请求时,Alamofire 会打印以下错误:
由于错误,无法序列化 JSON:无法读取数据,因为它的格式不正确。
以下是我试图运行的代码:
var parameters: [String: Any] = [:]
parameters[UserParameters.kUserId.rawValue] = self.userInfoForMe.user_id
parameters[UserParameters.kGender.rawValue] = self.userInfoForMe.gender
parameters[UserParameters.kfull_name.rawValue] = self.userInfoForMe.full_name
parameters[UserParameters.kDOB.rawValue] = self.userInfoForMe.dob
print("parameters:\(parameters)")
let userInfoDataHandler:UserInfoDataHandler = UserInfoDataHandler()
var DataJSON : JSON = JSON.null
userInfoDataHandler.postData(parameters: parameters){
responseObject, error in
if responseObject != nil{
DataJSON = JSON(responseObject!)
print(DataJSON)
}
else{
self.removeLoader(showNewController: false)
self.showAlertWithMessage(msg: ErrorContants.somethingWentWrongError)
}
}
这是我的 UserDataInfoHandler:
class UserInfoDataHandler: BaseService {
init(){
super.init(controller: Controllers.mobile_pa, apiName: ApiNames.save_user_info)
}
}
以下是我从基本服务调用的代码:
fileprivate func fetchData(method:String, endpoint:String,parameters: [String: Any],completionHandler: @escaping (NSDictionary?, Error?) -> ()){
//need to show loader here..
var request: DataRequest? = nil
let requestURL : String = URLConstants.baseURL + self.controller + "/" + self.apiName
switch method {
case RequestMethod.GET.rawValue:
request = Alamofire.request(requestURL, method: HTTPMethod.get, parameters: parameters)
case RequestMethod.POST.rawValue:
request = Alamofire.request(requestURL, method: HTTPMethod.post, parameters: parameters)
default:
print(ErrorContants.WentThroughSwitchCaseOf + "Base Service");
}
request?.responseJSON(completionHandler: { (response) in
//this block never gets executed as it throws error that JSON is not serialized.
self.checkForResponseAndError(response: response, completionHandler: completionHandler)
})
}
打印我的参数后,输出如下,
parameters:[ "full_name": "fatin", "user_id": "42", "dob": "06-12-1994", "gender": "1"]
更新:我尝试过使用相同代码处理其他发布请求和参数,它们工作正常。所以我开始知道我发送的参数有问题,但仍然无法弄清楚出了什么问题。