【问题标题】:Alamofire can't get request with phpAlamofire 无法使用 php 获取请求
【发布时间】:2017-07-25 10:34:38
【问题描述】:

我有这个 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)
)

【问题讨论】:

  • 更改编码试试?
  • 看起来像 JSON,你确定你的 API 没有返回 JSON?
  • @DávidPásztor Json encoding 给了我这个错误fAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) [Timeline]: Timeline: { "Request Start Time": 522672231.598, "Initial Response Time": 522672231.719, "Request Completed Time": 522672231.720, ...
  • JSON 请求无法接收到$_POST。如果你想让你的 Swift 代码正常工作,你可能需要更新你的 PHP 代码。
  • @OOPer 但在文档中 github.com/Alamofire/Alamofire 它说你应该像我一样发送帖子。

标签: swift alamofire


【解决方案1】:

试试这个代码

   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)")
        }

【讨论】:

  • 什么是回调?
  • 在回调中使条件得到 json ,数组或字符串,然后您可以填充并在此代码中字典回调得到
【解决方案2】:

您正在使用密钥对数组,因此使用.prettyPrinted 格式。 希望这个sn-p会有所帮助

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    相关资源
    最近更新 更多