gpt4 book ai didi

ios - swift : HTTP 500 Error when calling from callback

转载 作者:行者123 更新时间:2023-11-28 15:16:40 25 4
gpt4 key购买 nike

这是我正在使用的代码(登录部分执行成功,“登录成功”在控制台中可读)

@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 错误。(该解决方案也是 Not Acceptable ,因为我必须首先调用登录并在数据之后调用)

我在后端使用 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()
}

最佳答案

HTTP 500 是内部服务器错误,而不是客户端错误。本次API调用,服务器相关方法会出现异常。此错误通常表示服务器出现问题,需要查看错误跟踪。

关于ios - swift : HTTP 500 Error when calling from callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46728431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com