简体   繁体   中英

swift4: Garbage at end. error

Using Alamofire , swiftyJSON and swift4 , i'd got this error

error: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}))

when using calling this 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())
            }
    }
}

the response is in swift format when testing it in different sites, and this is the response

{
"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"
            }
        ]
    }
    ]
}

what is the problem here ?

1- Add this instead of nil header

let headers : HTTPHeaders = ["Content-Type":"application/json"]

2- Add this

.validate().responseJSON

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM