Swift 3 / Alamofire说失败但它有效

时间:2017-02-27 20:14:20

标签: php swift codeigniter alamofire

当我在xcode中打印响应时,我的alamofire请求回来失败但是,Web服务实际上正在执行此操作并且一切都应该发生,因为它应该除了它作为失败返回的事实

Alamofire.request(BASE_URL + "index.php/feed/like", method: .post, parameters: parameters, headers: headers).responseJSON { response in

            let result = response.result

            debugPrint(response)

            print(result)

            if (result.value != nil) {

                let json = JSON(result.value!)

                print(json)

                if json["status"] == false {

                    showError(type: "generic")

                } else {

                    let nolikes = Int(self.lblLikes.text!)! + 1

                    self.lblLikes.text = String(nolikes)

                    self.actionLike.image = UIImage(named: "ActionLiked")

                }

            }

        }

这是我的错误

[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 1." UserInfo={NSDebugDescription=Invalid value around character 1.}))
[Timeline]: Timeline: { "Request Start Time": 509902187.696, "Initial Response Time": 509902188.176, "Request Completed Time": 509902188.177, "Serialization Completed Time": 509902188.177, "Latency": 0.480 secs, "Request Duration": 0.480 secs, "Serialization Duration": 0.000 secs, "Total Duration": 0.481 secs }

这是我在codeigniter中的php函数

function like_post()
    {

        $post = $this->_post_args;

        $post = $this->security->xss_clean($post);

        $like = $this->Feed_model->likePost($post);

        if($like == TRUE) {

            $this->set_response([
                'status' => TRUE,
                'message' => 'SUCCESS'
            ], REST_Controller::HTTP_ACCEPTED);

        } else {

            $this->set_response([
                    'status' => FALSE,
                    'error' => 'ALREADY_LIKED'
                ], REST_Controller::HTTP_BAD_REQUEST);

        }

    }

public function likePost($post)
    {

        $postid = $post['post_id'];
        $userid = $post['user_id'];

        $query = $this->db->query("SELECT * FROM `post_likes` WHERE `user_id` = '{$userid}' AND `post_id` = '{$postid}'");

        if($query->num_rows() > 0) {

            return false;

        } else {

            $insert = [
                'user_id' => $userid,
                'post_id' => $postid
            ];

            $this->db->insert('post_likes', $insert);

            $currpost = $this->db->query("SELECT * FROM `posts` WHERE `post_id` = '{$postid}'");
            $likes = $currpost->likes;

            $update = ['likes' => $likes + 1];

            $this->db->update('posts', $update, "post_id = '{$postid}'");

            return true;

        }

    }

如果用户已经执行了此操作,代码将返回false,因此如果我第一次单击该操作,它将在控制台中作为失败返回,但php脚本将运行将所有内容插入数据库等,但是,如果我单击它再次我会得到php失败响应(所以请求显示成功,但它失败了)

0 个答案:

没有答案