swift - swift4:最后的垃圾。错误
问题描述
使用Alamofire
,swiftyJSON
和swift4
,我得到了这个错误
错误:responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误域 = NSCocoaErrorDomain 代码 = 3840 “垃圾结束。” UserInfo = {NSDebugDescription = 垃圾结束。}))
使用调用此 API 时
class func getLatestProducts( completion: @escaping (_ error: Error?, _ success: Bool,_ value: ErrorModel) -> Void) {
let url = URLs.latestProducts
print("url::: ", url)
Alamofire.request(url, method: .get, encoding: URLEncoding.default, headers: nil)
.responseJSON { reponse in
switch reponse.result {
case .failure(let error):
print("error: ", error)
completion(error, false, ErrorModel())
case .success(let value):
let json = JSON(value)
print(json)
completion(nil, true, ErrorModel())
}
}
}
在不同站点进行测试时,响应采用快速格式,这就是响应
{
"data": [
{
"id": 4,
"title": "Ut incidunt et dolorem inventore qui sunt harum.",
"price": "49",
"category_id": "13",
"modern": "1",
"fixed": "0",
"created_at": "2018-08-11 18:31:06",
"category": {
"id": 13,
"name": "Aliquam impedit.",
"icon": "LawnGreen"
},
"image": [
{
"id": 15,
"image": "https://lorempixel.com/640/480/?78973",
"product_id": "4"
}
]
},
{
"id": 24,
"title": "apple iPhone X",
"price": "5000",
"category_id": "28",
"modern": "0",
"fixed": "1",
"created_at": "2018-08-10 19:38:29",
"category": {
"id": 28,
"name": "قسم الجوالات",
"icon": "fas fa-mobile-alt"
},
"image": [
{
"id": 10,
"image": "m4cRboy.png",
"product_id": "24"
}
]
},
{
"id": 23,
"title": "Mollitia consectetur sed proident commodo ipsam eos dolorem harum magni consequatur aliquid quos dolorem optio consequuntur aut consequat",
"price": "203",
"category_id": "28",
"modern": "0",
"fixed": "1",
"created_at": "2018-08-08 06:54:11",
"category": {
"id": 28,
"name": "قسم الجوالات",
"icon": "fas fa-mobile-alt"
},
"image": [
{
"id": 9,
"image": "pXOdboy2.png",
"product_id": "23"
}
]
}
]
}
这里有什么问题 ?
解决方案
1-添加此而不是 nil 标头
let headers : HTTPHeaders = ["Content-Type":"application/json"]
2-添加这个
.validate().responseJSON
推荐阅读
- google-apps-script - 用于检索版本控制的 Google App 脚本
- vba - 子表单更新另一个子表单日期
- google-cloud-platform - Google Cloud Translate API 翻译返回 html 转义文本
- javascript - 将对象转换为二维数组但删除键
- php - 获取自定义帖子类型数据,包括 ACF 和 Timber?
- python - 读取数据框并转换为文本文件
- selenium - 如何使用 CTRL + S 使用 Selenium IDE 保存网页?
- docker - 找不到 docker-compose 正在运行
- java - 如何在 sqLite 中修复:无法从 CursorWindow 读取第 0 行第 0 列。确保在从光标访问数据之前正确初始化光标
- amazon-redshift - 在 Redshift 中控制 DELETE