如何获得对 post 字符串的响应,例如在注册时遇到错误
How to get the response to post string for eg in sign up i m facing error
我无法注册,当我尝试在控制台上打印时我没有收到回复,所以这是注册表单的代码,我现在无法注册:
var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
request.httpMethod = "POST"
let postString = "Email=\(textfieldemail.text!)&Password=\(textfieldpassword.text!)&MobileNo=\(textfieldmobile.text!)&UserType=\(butname)&LoginType=Normal"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let msg = json.value(forKey: "message") as! NSString!
self.activityIndi.stopAnimating()
self.activityIndi.hidesWhenStopped = true
let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
task.resume()
}
我尝试运行你的代码示例输入
var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
request.httpMethod = "POST"
let email: String = "pardeep@yopmail.com"
let password: String = "12345678"
let mobileNo: String = "9599904029"
let butname: String = "1"
let postString = "Email=\(email)&Password=\(password)&MobileNo=\(mobileNo)&UserType=\(butname)&LoginType=Normal"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let msg = json.value(forKey: "message") as! NSString!
let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
task.resume()
其响应状态不正确,请与您的后端开发人员核实输入和URl。
response = Optional(<NSHTTPURLResponse: 0x604000425fe0> { URL: http://fundedindia.org/funded/Doner_Registration.php } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 5759;
"Content-Type" = "text/html; charset=utf-8";
Date = "Thu, 07 Dec 2017 08:58:40 GMT";
Server = "Microsoft-IIS/8.0";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} })
"status code: 500" 应该是 200 才算成功。
我无法注册,当我尝试在控制台上打印时我没有收到回复,所以这是注册表单的代码,我现在无法注册:
var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
request.httpMethod = "POST"
let postString = "Email=\(textfieldemail.text!)&Password=\(textfieldpassword.text!)&MobileNo=\(textfieldmobile.text!)&UserType=\(butname)&LoginType=Normal"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let msg = json.value(forKey: "message") as! NSString!
self.activityIndi.stopAnimating()
self.activityIndi.hidesWhenStopped = true
let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
task.resume()
}
我尝试运行你的代码示例输入
var request = URLRequest(url: URL(string: "http://fundedindia.org/funded/Doner_Registration.php")!)
request.httpMethod = "POST"
let email: String = "pardeep@yopmail.com"
let password: String = "12345678"
let mobileNo: String = "9599904029"
let butname: String = "1"
let postString = "Email=\(email)&Password=\(password)&MobileNo=\(mobileNo)&UserType=\(butname)&LoginType=Normal"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let json = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary
let msg = json.value(forKey: "message") as! NSString!
let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
task.resume()
其响应状态不正确,请与您的后端开发人员核实输入和URl。
response = Optional(<NSHTTPURLResponse: 0x604000425fe0> { URL: http://fundedindia.org/funded/Doner_Registration.php } { status code: 500, headers {
"Cache-Control" = private;
"Content-Length" = 5759;
"Content-Type" = "text/html; charset=utf-8";
Date = "Thu, 07 Dec 2017 08:58:40 GMT";
Server = "Microsoft-IIS/8.0";
"X-Powered-By" = "ASP.NET";
"X-Powered-By-Plesk" = PleskWin;
} })
"status code: 500" 应该是 200 才算成功。