[英]Alamofire request parameters empty
我正在嘗試使用Alarmofire庫發送POST請求,但請求不會正確發送參數。
我的代碼:
let parameters : Parameters = [
"email": tfLoginEmail.text! as String,
"password": tfLoginPassword.text! as String
]
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON{ response in
//Some code that uses the response
}
參數變量的計數為2且兩個值都存在,但對此請求的響應是關於電子郵件和/或密碼為空的錯誤。
編輯:我的PHP:
/**
* Account Login
* url - /login
* method - POST
* params - email, password
*/
$app->post('/login', function() use ($app) {
// check for required params
verifyRequiredParams(array('email', 'password'));
// reading post params
$email = $app->request()->post('email');
$password = $app->request()->post('password');
$response = array();
$db = new DbHandler();
// check for correct email and password
if ($db->checkLogin($email, $password)) {
// get the user by email
$account = $db->getAccountByEmail($email);
if ($account != NULL) {
$response["error"] = false;
$response['id'] = $account['id'];
$response['name'] = $account['name'];
$response['email'] = $account['email'];
} else {
// unknown error occurred
$response['error'] = true;
$response['message'] = "An error occurred. Please try again";
}
} else {
// user credentials are wrong
$response['error'] = true;
$response['message'] = 'Login failed. Incorrect credentials';
}
echoRespnse(200, $response);
});
我想知道我做錯了什么。 提前致謝。
顯然,服務器期望請求主體是URL編碼的字符串,而不是JSON。 使用encoding: URLEncoding.httpBody
而不是encoding: JSONEncoding.default
來解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.