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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题