Search code examples
iosalamofire

AlamoFire isn't sending the params to PHP


Here is my AlamoFire code

let parameters = ["test": "true"]

Alamofire.request(urlString, method: .post, parameters: parameters ,encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
  switch response.result {
                case .success:
                    print(response)
                    break
                case .failure(let error):

                    print(error)
                }
}

An example of my PHP code is here. I always get the error. Is it my PHP or AlamoFire?

if($_POST['test'] == 'true'){
    $json = json_encode(array(
        "ack" => "success",
        "message" => "good"
    ));
      echo $json;
} else {
    $json = json_encode(array(
        "ack" => "error",
        "message" => "not good"
    ));
      echo $json;
}

Solution

  • Try change your encoding for URLEncoding.default

    AF.request(url, method: .post, parameters: p, encoding: URLEncoding.default).responseJSON {