Get JSON result with GET request and parameters with Alamofire

泄露秘密 提交于 2019-12-01 06:29:20

i think you should remove the parameter of "encoding: ParameterEncoding.JSON",like this:

Alamofire.request(.GET, strURL, parameters: params).responseJSON { (responseObject) -> Void in

        print(responseObject)

        if responseObject.result.isSuccess {
            let resJson = JSON(responseObject.result.value!)
            success(resJson)
        }
        if responseObject.result.isFailure {
            let error : NSError = responseObject.result.error!
            failure(error)
        }


}

Your requestGETURL should look like that

    func requestGETURL(strURL: String, params: [String:String]?, success: (AnyObject?) -> Void, failure: (NSError) -> Void) {
    Alamofire.request(.GET, strURL, parameters: params).responseJSON {
        (responseObject) -> Void in

        print(responseObject)

        if responseObject.result.isSuccess {
            let resJson = JSON(responseObject.result.value!)
            success(resJson)
        }
        if responseObject.result.isFailure {
            let error: NSError = responseObject.result.error!
            failure(error)
        }


    }
}

Your problem was in params it should be [String:String] dictionary. Also you don't have to declare encoding encoding:ParameterEncoding.JSON.

Hope it help you

Use this code. It is retrieving response correctly parsed in JSON.

Using Alamofire v3.0+

Alamofire.request(.GET, "http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1")
        .responseJSON { response in
            debugPrint(response)

            switch response.result {
            case .Success(let JSON):
                print(JSON)
            case .Failure(let error):
                print(error)
            }
    }

EDIT: For accepting Parameters with GET Type Service:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
     .responseData { response in
         print(response.request)
         print(response.response)
         print(response.result)
      }

In this case try to not manipulate your URL String and send all parameters in terms of Dictionary like this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!