簡體   English   中英

swift4:垃圾結束。 錯誤

[英]swift4: Garbage at end. error

使用AlamofireswiftyJSONswift4 ,我遇到了這個錯誤

錯誤: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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM