【发布时间】:2017-06-16 12:06:43
【问题描述】:
大家好,我在 swift 3 iphone 程序上使用 alamofire,我的问题是我需要从 php 页面返回一个值,我的问题是返回给我的值就是这个。如何确保我返回的值是:no prova@email.it
希望我已经解释过了
返回值(不正确):
SUCCESS: {
message = "no Optional(\"prova@email.it\")";
} 没有可选的(“prova@email.it”)
SWIFT 代码:
import Foundation
import Alamofire
class User{
//URL to our web service
var email=""
var password=""
func PrintValue(){
// print(username);
//print(password);
}
func Login() -> String{
//var ris="";
var readvalue=""
let URLString = "http://localhost/test/login_mobile.php"
let parameters_value: Parameters = [
"email": email,
"password": password
]
//Sending http post request
Alamofire.request(URLString, method: .post, parameters: parameters_value).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let result = response.result.value {
//converting it as NSDictionary
let jsonData = result as! NSDictionary
//displaying the message in label
readvalue = (jsonData.value(forKey: "message") as! String?)!
print(readvalue)
}
}
return readvalue
}
}
PHP 代码:
<?php
include 'user.php';
header('Content-Type: application/json');
$email= $_POST['email'];
$password = $_POST['password'];
$ris['message']="";
$user = new User();
//procedo con il login
if($user->login($email,$password,"MOBILE")==true){
$ris['message']="yes";
}
else{
$ris['message']="no $email";
}
echo json_encode($ris);
?>
【问题讨论】:
-
你的答案是可选的吗?
readvalue! -
但如果我删除!我有一个错误:@AgRizzo
-
@AgRizzo 所以,你需要接收“prova@email.it”而不是可选的吗?
-
@SergeyDi 是的!!!