我正在尝试如下创建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>";
}
答案 0 :(得分:0)
问题是您正在使用Alamofire通过以下方式发送数据
JSONEncoding.default
,并希望后端的数据作为URL编码。将编码更改为:
URLEncoding.default
你应该很好。