CakePHP的AJAX的Json解析错误使用模型时,模型、错误、CakePHP、AJAX

2023-09-10 22:13:49 作者:倚楼醉听雨

我正在开发使用CakePHP 2.5.3.0的应用程序,然后我偶然发现了一个问题,AJAX:

I'm developing an application using CakePHP 2.5.3.0, and then I stumbled upon a problem with AJAX:

我用一个AJAX请求的jQuery来CakePHP的发送用户的登录名和密码,然后CakePHP的应该返回验证JSON响应。现在的问题是:当我用在我的控制器的操作方法模型方法,JSON响应出来的JSON开始前意外字符

I'm using an AJAX request from jQuery to CakePHP to send the user's login and password, and then CakePHP should return a JSON response validated. The problem is: Whenever I'm using a Model method within my action method in the Controller, the JSON response comes out with an unexpected character before the start of the JSON.

下面是看到回应谷歌浏览器时的屏幕截图: http://i.imgur.com/m5x6X4G.png

Here's a screenshot when seeing response on Google Chrome: http://i.imgur.com/m5x6X4G.png

jQuery的AJAX请求code是在这里:

The jQuery AJAX Request Code is here:

        $.ajax({
            url: "/login/signin.json",
            cache: false,
            type: "POST",
            dataType: "json",
            data: {
                email: $("#login-form").find("input[name=email]").val(),
                password: $("#login-form").find("input[name=password]").val()   
            },
            success: function(response) {               
                self.callback.login(response);
            }
        }); 

这里是的LoginController的登入资讯的方法:

And here is the LoginController's "signin" method:

        public function signin() {
        if(!$this->request->is("ajax"))     
            throw new BadRequestException();

        $this->layout = 'ajax';
        $this->response->disableCache();

        $this->RequestHandler->respondAs("application/json");

        if($this->request->is("post")):             
            $account = $this->Account->validateAccount($this->request->data['email'], Security::hash($this->request->data['password'],"sha1",true));

            if(count($account)>0):
                $account = $account['Account'];
                $message = array(   "success" => true,
                                    "message" => "[]");

                $AccountManager = new AccountSessionManager();
                $AccountManager->setId($account['id_account']);
            else:
                $message = array(   "error" => true,
                                    "message" => "The entered e-mail or password are invalid",
                                    "code" => 2 );
            endif;
        else:
            $message = array(   "error" => true,
                                "message" => "No POST request.",
                                "code" => 1 );
        endif;  

        $this->set("message", $message);
        $this->set("_serialize", array('message'));

        $this->render("ajax");
    }       

对于我在上面呈现的Ajax的视图文件是一个简单

The view file for "ajax" which I rendered above is a simple

<?php echo $message ?>

但后来,每当我改变这一行

But then whenever I change this line

$account = $this->Account->validateAccount($this->request->data['email'], Security::hash($this->request->data['password'],"sha1",true));

$account = array();

我不明白JSON响应之前那个奇怪的字符。

I don't get that strange character before the json response.

我不知道为什么会这样,但出现这种情况的仅当我使用任何控制器内的模型方法...

I have no clue why this happens, but this happens only when I use any Model methods within the Controller...

顺便说一句,我从JavaScript得到的错误是:

By the way, the error I get from javascript is:

未捕获的SyntaxError:意外的标记

Uncaught SyntaxError: Unexpected token

和意想不到的角色,我说的是小红点在Chrome的JSON之前。

And the unexpected character I'm talking about is the little red dot in Chrome before the json.

推荐答案

这字符出现,因为你的PHP文件中的一个对换行的空间开放的PHP标签之前

只是检查你的模型或的configs在任何发现额外的换行符或空格。

That character appears because one of your php files have space on newline before open php tag Ajax和JSON

just check your models or configs on whatever to find extra newline or space.

此外 BOM字符会产生这样的结果。 PHP文件不应该有任何BOM。如果你已经使用Windows记事本编辑你的文件是BOM是肯定的。只需使用编辑器程序之一。

Also BOM character can produce such result. PHP files should not have any BOM. If you have used Windows Notepad to edit you files it's BOM for sure. Just use one of editors for programming.

你问之前,GOOGLE了? JSON响应格式错误 - 红点\响应

Have you googled before asking? JSON response format error - red dot\bullet before response