Alamofire无法通过php

时间:2017-07-25 10:34:39

标签: swift alamofire

我有这个Swift 4代码

let par: Parameters = [
                          "usn":"Murad",
                          "Password":"monkey"
                      ]
Alamofire.request("http://www.web.com/ajax/logreg.php",method: .post,parameters:par,encoding: URLEncoding.httpBody).response { response in

    if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
       print("Data: \(utf8Text)")
   }
}

这个PHP代码

print_r($_POST);

现在我认为这段代码会打印

Array(
   "usn":"Murad",
   "Password":"monkey",    
)

但相反它打印

Array
(
    [(componentTuple_0)] => (componentTuple.1)
)

2 个答案:

答案 0 :(得分:0)

试试这段代码

   let par: Parameters = [
                "usn":"Murad",
                "Password":"monkey"
            ]


   Alamofire.request("http://www.web.com/ajax/logreg.php", method: .post, parameters: par as? [String : AnyObject], encoding: JSONEncoding.default).responseJSON { response in
            switch response.result {
            case .success(let JSON):
                //                    print("Success with JSON: \(JSON)")
                if let response = JSON as? NSMutableDictionary
        {
            callback(response,nil)
        }
        else if let response = JSON2 as? NSDictionary
        {
            callback(response,nil)
        }
        else if JSON2 is NSArray
        {


        }
        else if ((JSON as? String) != nil)
        {

            let userDic : [String : String] = ["quatid":JSON as! String]
            print(userDic)
            callback(userDic as NSDictionary?,nil)

    }

                break

            case .failure(let error):
                print("Request failed with error: \(error)")
                callback(response.result.value as? NSMutableDictionary,error as NSError?)
                break
            }
            }
            .responseString { response in
                print("Response String: \(response.result.value)")
        }

答案 1 :(得分:0)

您正在使用密钥对数组,因此使用.prettyPrinted格式。 希望这段代码能够帮助

     var arrayOfKeyPairs = [[String:Any]]()
     let JSON = try? JSONSerialization.data(withJSONObject: arrayOfKeyPairs, options: [.prettyPrinted])
     let jsonPrasatation = String(data: JSON!, encoding: .utf8)