这是我正在使用的代码(登录部分成功执行,“登录成功”在控制台中可读)
@IBAction func loginAction(_ sender: Any) {
print("loginIsExecuting")
Server.Login(email: inputEmail.text!, password: inputPassword.text!, handlerDone:{
print("Logged in successfully")
Help.getData();
}, handlerFailed: {
print("===Failed to log in ")
DispatchQueue.main.sync {
self.displayAlert()
}
})
}
Help.swift
import Foundation
class Help {
static func getData(){
let urlString = URL(string: "http://192.168.1.16/~peterlizak/Secway/web/app/get-auditor")
if let url = urlString {
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error)
} else {
let responseString = String(data: data!, encoding: .utf8)
print(responseString) //JSONSerialization
}
}
task.resume()
}
}
}
执行时我收到500内部服务器错误。 这是来自apache日志:
192.168.1.16 - - [13/Oct/2017:12:04:30 +0200] "POST /~Myname/Myapp/web/app/get-auditor HTTP/1.1" 500 495
使用相同的代码仅在 Server.login 之后调用 Help.getData() (不在当我只执行一次代码时,回调函数正在工作。但是,如果我再次单击该按钮以触发 loginAction() ,则该功能将再次像第一个版本中那样运行:登录执行,“已成功登录“在控制台中,但是当代码尝试执行HTTP请求时,它会收到相同的HTTP 500错误。(此外,该解决方案也不可接受,因为我必须首先登录并在数据之后调用)
我正在使用symphony作为后端,这是我尝试请求的简单代码:
/**
* @Route("/get-auditor" , name="get_auditor")
*/
public function getAuditorInfoAction(Request $request)
{
$data = array(
'status' => 1,
'description' => 1,
"header" => 1
);
return new JsonResponse($data);
}
修改
Server.Login
static func Login(email: String , password: String ,handlerDone:@escaping () -> Void,handlerFailed:@escaping () -> Void) {
print("=== Server Login ===")
let url = URL(string: "\(self.baseUrl)~peterlizak/Secway/web/app/login")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
let parameters = ["email": email, "password": password]
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
CallService(request: request, handlerDone:{ json in
ParseLoginResponse(json: json)
handlerDone()
}, handlerFailed: {
handlerFailed()
} )
}
Server.CallService
static func CallService(request:URLRequest,handlerDone: @escaping (_ json:[String:Any]) -> Void,handlerFailed: @escaping () -> Void)
{
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))")
handlerFailed()
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 = \(response)")
handlerFailed()
return
}
let responseString = String(data: data, encoding: .utf8)
var json:[String:Any]
do {
json = (try JSONSerialization.jsonObject(with: data, options: [])) as! [String:Any]
}
catch {
print("json error calling "+(request.url?.absoluteString)!+" =\(String(describing: error))")
print("responseString = \(responseString!)")
handlerFailed()
return
}
let status = json["status"] as! Int
if status != 0{
handlerDone(json)
}
else{
print("status is 0 = \(responseString!)")
handlerFailed()
}
}
task.resume()
}
答案 0 :(得分:1)
HTTP 500是内部服务器错误,而不是客户端错误。服务器中与此API调用相关的方法中会有一些例外。此错误通常表示服务器出了点问题,需要调查错误跟踪。
答案 1 :(得分:0)
您很可能会向错误的网址发送请求,因为您的代码/~peterlizak/Secway
并且错误回复有/~Myname/Myapp