SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

I have the following AJAX call:

            if($(this).val().length > 2) {
                    url: '<?php echo $html->url('/fonykers/validate_username',true); ?>' + '/' + $(this).val(),
                    dataType: 'json',
                    type: 'POST',
                    success: function(response) {
                        if(!response.ok) {
                        } else {
                            if($(this).is('.error')) {
                                alert('im in');
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('You fail');
            } else {
               error.html('Username must have at least 3 characters');

And this is the method it's calling:

function validate_username($username) {
        $this->layout = 'ajax';
        $response = array();
        $fonyker = $this->Fonyker->find('first', array(
            'conditions' => array('Fonyker.username' =>  $username)

        if(!strlen($username) > 2) {
            $response = json_encode(array('ok' => false, 'msg' => 'Username must have at least 3 characters'));
        } else if(!preg_match('"^[a-zA-Z0-9]+$"', $username)) {
            $response = json_encode(array('ok' => false, 'msg' => 'Username must be alphanumeric'));
        } else if ($fonyker) {
            $response = json_encode(array('ok' => false, 'msg' => 'Username is already in use'));
        } else {
            $response = json_encode(array('ok' => true, 'msg' => ''));

        echo $response;

In the AJAX call it always goes to the error part and outputs that error, and I don't see any whitespaces in my PHP code. Any thoughts?


What was happening is that CakePHP was echoing the entire view in the response, but not really the view but the error screen.

If you need AJAX methods in your controller that do not require a view you must set the $this->autoRender property on the controller method to false before doing anything.


Based on OP's comment response:

Although I'm not specifically familiar with the CakePHP way of doing it, when using ZendApplication, one of the first things to do is make an empty layout. Normal responses get placed in the default layout, while ajax responses get placed in the empty layout.

In your controller methods, set $this->layout to your empty layout to override the default where applicable.

See for more specific instructions

There may have been an echo in your ajax function. Check it and remove any echo, print, debug from the ajax function.

When you return json data, you can't echo anything.

Did you check ajax.ctp file in layouts folder inside view folder?

When using ColdFusion for development, be sure to turn debugging OFF or else it can cause this issue

Your client/ajax call expects a JSON dataType from the server. Now you need to make sure the server is returning a JSON dataType, else change the request to request to reflect what the service is returning.

To investigate this, use firebug to see what you are getting as response. I just got this error and I fixed it.

i had the same problem, but i solved with "$this->autoRender = false;" in the function!

Need Your Help

Validate IE Specific CSS

css internet-explorer

I have inherited the following CSS for a site, and I can't tell if it does anything useful. It is included as IE specific if the IE version is less than 8. So my guess is its an attempt to handle P...

UIColor of tab bar items unselected and changing tabbar color

ios ios8 uitabbar uicolor uitabbaritem

I see by using the setTintColor on a tabBar I can change the selected item color.