Alamofire printing "JSON could not be serialized because of error:"
Alamofire printing "JSON could not be serialized because of error:"
我正在尝试使用一些参数发出 post 请求,但是在发出请求时 Alamofire 打印出以下错误:
JSON could not be serialized because of error:
The data couldn’t be read because it isn’t in the correct format.
以下是我正在尝试的代码 运行:
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"]
更新:我已经尝试使用相同代码处理其他 post 请求和参数,它们工作正常。所以我开始知道我发送的参数有问题,但仍然无法弄清楚出了什么问题。
经过数小时的思考和与我的团队讨论,发现我的代码没有任何错误。问题是,服务器端进行了一些验证,因为在响应中出现 PHP 错误,alamofire 无法处理(因为 alamofire 只能处理 JSON 响应).请在 Postman 中查找附件中的错误截图。
所以实际的问题是来自服务器的响应是 post 请求中的 PHP 错误 。现在我得到了正确的回应,因为现在没有问题。
希望有人会觉得这有帮助。
不要使用 responseJSON,而是使用 responseString。对我有用。
responseString(completionHandler: { (response) in
//this block never gets executed as it throws error that JSON is not serialized.
self.checkForResponseAndError(response: response, completionHandler: completionHandler)
})
我正在尝试使用一些参数发出 post 请求,但是在发出请求时 Alamofire 打印出以下错误:
JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format.
以下是我正在尝试的代码 运行:
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"]
更新:我已经尝试使用相同代码处理其他 post 请求和参数,它们工作正常。所以我开始知道我发送的参数有问题,但仍然无法弄清楚出了什么问题。
经过数小时的思考和与我的团队讨论,发现我的代码没有任何错误。问题是,服务器端进行了一些验证,因为在响应中出现 PHP 错误,alamofire 无法处理(因为 alamofire 只能处理 JSON 响应).请在 Postman 中查找附件中的错误截图。
不要使用 responseJSON,而是使用 responseString。对我有用。
responseString(completionHandler: { (response) in
//this block never gets executed as it throws error that JSON is not serialized.
self.checkForResponseAndError(response: response, completionHandler: completionHandler)
})