Alamofire从POST获得回复

时间:2016-02-27 10:08:40

标签: swift post alamofire

如何使用Alamofire从POST方法获得响应? 我有下一个POST方法:

let parameters = [
                  "Firstname":  "\(first_name)",
                  "Lastname": "\(last_name)",

                 ];
 Alamofire.request(.POST, URLString, parameters: parameters as? [String : AnyObject], encoding: .JSON)

完成后,我想通过ID和其他字段获得我的记录回复。我该怎么办?

2 个答案:

答案 0 :(得分:0)

你可以写一个像这样的方法......

func getData(result: (response: NSMutableArray!, error : NSError!) -> Void){

let parameters = [
                  "Firstname":  "\(first_name)",
                  "Lastname": "\(last_name)", ];        

    //Request to fetch data from the server
    Alamofire.request(.POST,URLString, parameters: parameters,encoding:.JSON).responseJSON
       { response in switch response.result {
           case .Success(let jsonData):
                result(response: jsonData  as! NSMutableArray,  error: nil)
           case .Failure(let error):
                result(response: nil, error: error)
           }
       }

}//getData

并将上述方法称为......

 YOURCLASSOBJECT.getFeedData( { (response, error) -> Void in

        //If data is fetched successfully
        if(response != nil){

          print("Response : \(response)")
        }  
})

答案 1 :(得分:0)

    <div class="input-field">
        <select name="color" id="color">
            <option value="%23B1365F">Pink</option>
            <option value="%232952A3">Blue</option>
            <option value="%23711616">Red</option>
            <option value="%2328754E">Green</option>
            <option value="%23BE6D00">Orange</option>
            <option value="%23113F47">Sea Blue</option>
            <option value="%235229A3">Purple</option>
            <option value="%23528800">Olive</option>
            <option value="%2388880E">Gold</option>
            <option value="%23333333">Black</option>
        </select>
        <label for="color">Colour: </label>
    </div>