0

我正在尝试创建一个 Alamofire 请求,如下所示:

//creating parameters for the post request
        let parameters: Parameters=[
            "modelo": modelo_id
        ]
        let url_dispo = URL(string: "https://../calcular_precio.php")

        Alamofire.request(url_dispo!, method: .post, parameters: parameters, encoding: JSONEncoding.default)
                        .responseJSON { response in
                            print(response)
            //to get status code
                            if let status = response.response?.statusCode {
                                switch(status){
                                case 201:
                                    print("example success")
                                default:
                                    print("error with response status: \(status)")
                                }
                            }
            //to get JSON return value
                        if let result = response.result.value {
                            let JSON = result as! NSDictionary
                            print(JSON)
                        }

                    }

我的问题是来自 url 的响应告诉我 post 参数没有随请求一起发送。

我检查了 modelo_id 的值,它是正确的。

我的代码有什么问题吗?

这里有完整的 PHP 脚本:

 <?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['modelo']) ) {

    // receiving the post params
    $modelo= $_POST['modelo'];


    // get the user by email and password
    $user = $db->getPrecioByModelo($modelo);

    if ($user != false) {
        // use is found
        $response["error"] = FALSE;
        $response["id"] = $user["id"];
        $response["precio"]["p1"] = $user["p1"];
        $response["precio"]["p2"] = $user["p2"];
        $response["precio"]["p3"] = $user["p3"];
        $response["precio"]["p4"] = $user["p4"];
        $response["precio"]["p5"] = $user["p5"];
        $response["precio"]["p6"] = $user["p6"];
        $response["precio"]["p7"] = $user["p7"];
        $response["precio"]["p8"] = $user["p8"];
        $response["precio"]["p9"] = $user["p9"];
        $response["precio"]["p10"] = $user["p10"];
        $response["precio"]["p11"] = $user["p11"];
        $response["precio"]["p12"] = $user["p12"];
        $response["precio"]["p13"] = $user["p13"];
        $response["precio"]["p14"] = $user["p14"];
        $response["precio"]["p15"] = $user["p15"];
        $response["precio"]["p16"] = $user["p16"];
        $response["precio"]["p17"] = $user["p17"];
        $response["precio"]["p18"] = $user["p18"];
        $response["precio"]["p19"] = $user["p19"];
        $response["precio"]["p20"] = $user["p20"];
        $response["precio"]["p21"] = $user["p21"];
        $response["precio"]["p22"] = $user["p22"];
        $response["precio"]["p23"] = $user["p23"];







        echo json_encode($response);
    } else {

        $response["error"] = TRUE;
        $response["error_msg"] = "Wrong action! Please, try again!";
        echo json_encode($response);
    }
} else {
    // required post params is missing
    $response["error"] = TRUE;
    $response["modelo"] = $modelo;
    $response["error_msg"] = "Required parameters missing!";
    echo json_encode($response);
}
?>

编辑:

这里有调试器输出:

modeloid  1069
SUCCESS: {
    error = 1;
    "error_msg" = "Required parameters email or password is missing!";
    modelo = "<null>";
}
error with response status: 200
{
    error = 1;
    "error_msg" = "Required parameters email or password is missing!";
    modelo = "<null>";
}
4

1 回答 1

0

问题是您使用 Alamofire 发送数据

JSONEncoding.default

并期望后端的数据作为 URL 编码。将编码更改为:

URLEncoding.default 

你应该很高兴。

于 2020-05-23T18:11:49.243 回答