8vius
8vius

Reputation: 5836

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

I have the following AJAX call:

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

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?

EDIT:

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.

Upvotes: 2

Views: 17500

Answers (6)

Cocuba
Cocuba

Reputation: 372

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

Upvotes: 0

eadjei
eadjei

Reputation: 2096

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.

Upvotes: 0

ttemple
ttemple

Reputation: 1995

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

Upvotes: 0

Hanif
Hanif

Reputation: 21

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.

Upvotes: 2

Passionate Engineer
Passionate Engineer

Reputation: 10412

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

Upvotes: 0

Jonathan Fingland
Jonathan Fingland

Reputation: 57167

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 http://www.balistupa.com/blog/2009/07/how-to-change-cakephp-layout/ for more specific instructions

Upvotes: 1

Related Questions