dongzhan2029 2018-11-19 11:26
浏览 73
已采纳

如何将验证错误从php或segue返回swift成功

I am using alamofire to post data to a php script which inserts data into a mysql database. This part is working fine but I have some validation on the server like checking for a valid email address for example. So, here is the swift code:

    Alamofire.request(url!, method: .post, parameters: parameters)
        .validate(statusCode: 200..<300)
        .responseJSON { response in
            switch response.result {
            case .success:
                print(response)
            case .failure(let error):
                print(error)
            }
    }

Here is the php that handles the inserting.

    if($booking->create($conn)) {

        $response['status'] = "200";
        $response['message'] = "Success";

    } else {

        $response['status'] = "400";
        $response['message'] = $booking->errors;
    }

    echo json_encode($response);

In swift, the response in console for failed validation is:

SUCCESS: {
    message =     (
        "Invalid email address",
        "Contact number required"
    );
    status = 400;
}

If there are no errors, I actually want to segue to another view controller and if there are errors, I want to display them in labels perhaps.

But, initially I just wanted to say if there are any errors, print them and if there are no errors, then segue. But if I try to put an if statement into the response I get an error.

 case .success:
    if response == "200" {
    // segue
 }

Binary operator '==' cannot be applied to operands of type 'DataResponse' and 'String'

  • 写回答

1条回答 默认 最新

  • dongpao5127 2018-11-20 09:48
    关注

    I am hoping someone will show me a better way to do this but I have made some progress and it is working. My understanding of the Alamofire documentation is that if you get a response that means it will always be a 200 response no matter what your server sends back. So, the only way I could work around that was to parse the response as json using codable and then do something based on that data, like this:

            Alamofire.request(url!, method: .post, parameters: parameters)
                .responseJSON { response in
                    let data = response.data
                    let jsonDecoder = JSONDecoder()
    
    
                    do {
    
                        let serverResponse = try jsonDecoder.decode(PhpResponse.self, from: data!)
                        if serverResponse.status == "200" {
    
                            print("Success")
    
                        } else {
    
                            print(serverResponse.message)
                        }
    
                    } catch {
    
                        debugPrint(error.localizedDescription)
    
                    }
            }
    

    So, I post the data from swift to the server, do some form validation using php and then using php's json_encode, I send that back to swift in json format and do something based on that response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog