Swift using Alamofire

swift simple network request, Xueyibujing, if wrong, please point out

The following is a method I package, the official address directly copy over https://github.com/SwiftyJSON/SwiftyJSON , no problem, because there is no written argument, so there is no problem

   func getRequestData(url:String,parame:[String:String]){
        Alamofire.request(url).responseJSON { response in
            print(response);
        };
    }

 It is a parameter of the get request data, the official code direct pull over,

func getRequestData(url:String,parame:[String:String]){
        Alamofire.request(url, method: .get, parameters: parame, encoding: JSONEncoding.default, headers: nil).responseJSON { (response) in
            if response.result.isSuccess{
                print("成功了")
                if let value = response.result.value as? [String: AnyObject]{
                    print("返回的结果:\(value) : \(value["msg"]!)")
                    
                    
                }
            }
    }

 Background data has not, will be given FAILURE: responseSerializationFailed (Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed (Error Domain = NSCocoaErrorDomain Code = 3840 "Invalid value around character 0." UserInfo = {NSDebugDescription = Invalid value around character 0.}) ) , check the information found if the receive mode after the request is based on post, back when the receiving mode is from the form encoding must be as URLEncoding.queryString. Otherwise, the background is not receiving data

Change

        Alamofire.request(url, method: .get, parameters: parame, encoding: URLEncoding.queryString, headers: nil).responseJSON { (response) in
            if response.result.isSuccess{
                print("成功了")
                if let value = response.result.value as? [String: AnyObject]{
                    print("返回的结果:\(value) : \(value["msg"]!)")
                }
            }
        }

 

Then things perfect solution

 

Guess you like

Origin www.cnblogs.com/hualuoshuijia/p/11760371.html