【发布时间】:2017-11-30 06:37:49
【问题描述】:
我是初学者,在手机上部署我的应用程序时遇到了问题。该应用程序在模拟器上运行良好,但是当我在手机上运行并尝试在手机上发送注册表时,出现以下错误
FAILURE: Error Domain=NSURLErrorDomain Code=-1004 “无法连接到服务器。” UserInfo={NSUnderlyingError=0x174244da0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost/Syafiqah_153046u/register.php, NSErrorFailingURLKey=http://localhost/Syafiqah_153046u/register.php, _kCFStreamErrorDomainKey=1, =61, NSLocalizedDescription=无法连接到服务器。}
下面是我的源码
class ViewController: UIViewController {
@IBOutlet weak var username: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var fullname: UITextField!
@IBOutlet weak var phone: UITextField!
@IBOutlet weak var labelMessage: UILabel!
@IBAction func Register(_ sender: UIButton)
{
let URL_USER_REGISTER = "http://localhost/FYP/register.php"
let parameters: Parameters=[
"username":username.text!,
"password":password.text!,
"email":email.text!,
"fullname":fullname.text!,
"phone":phone.text!
]
//Sending http post request
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).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
self.labelMessage.text = jsonData.value(forKey: "message") as! String?
}
}
}
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
您的帮助将不胜感激。感谢您的宝贵时间
【问题讨论】:
-
“无法连接到服务器。”当然:
http://localhost/FYP/register.php... "localhost" ... 本地 -
尝试本地IP地址而不是localhost..希望对你有帮助
-
天哪,它成功了!非常感谢@Jaydeep
-
谢谢你让我意识到我的愚蠢错误!! @EricAya
-
@nrsyaf 不客气。 :)