2

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.

3
  • 2
    the error is for a non-whitespace character, not a whitespace character. Also, could you post the resultant json? Use Firebug (or the like) to observe the response. Commented Jul 20, 2011 at 19:42
  • Looking at the response I see that cake is sending back the entire page HTML with the JSON response, because it says it requires a view. Commented Jul 20, 2011 at 19:52
  • You shouldn't be echoing out things from within your controller method. That's what views are for. The technically correct way to approach this would be to set a response variable here, and echo it in the view. Its a little tedious to create a whole new file to echo out one line, so you can also return $response from this method and you'll get the response in your ajax call. Both are preferable to echoing in a controller method I would think. Commented Jan 11, 2013 at 7:31

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

In the end it was setting $this->autoRender = false; on the controller method to set that it doesn't have a view, you were the most help, Jonathan, thank you, I'll edit my question with the answer but I'll accept yours for setting me on the right path.
Glad I could help, even if it wasn't the specific solution. Thanks as well for editing your question so it can help others in the future
0

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

Comments

0

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.