0

Searching for an answer for this question I got as a result that follwing code works fine:

xhr = new XMLHttpRequest();
xhr.onreadystatechange=function()
{
    if (xhr.readyState==4 && xhr.status==200)
    {
        response = JSON.parse(xhr.responseText);
        if(typeof response =='object') {
            $('#modal-spinner-seo-update').hide('slow');
            jQuery.each(result, function(field, message) {
                $('#seo-'+field).next('div.error-message').html(message).fadeIn('fast');
            });
        } else {
            $('#modal-spinner-seo-update').hide('slow', function() {
                $("#seo-widget-message-success").fadeIn('slow').delay(2000).fadeOut('slow');
            });
        }
        return false;
    }
};
xhr.open('GET','/metas/saveMetas?model='+model+'&f_key='+f_key+'&pagetitle='+pagetitle+'&keywords='+keywords+'&description='+description+'&niceurl='+niceurl, true );
xhr.send();

but this jQuery Version does not work. So can anyone spot the mistake? Is there any? The jQuery AJAX version works fine on my localhost but the server it does not, but return an 403 Forbidden Error. It is a cakePHP project.

So I hope someone ca tell me whats wrong or what setting is missing.

$.ajax({
    url: '/metas/saveMetas',
    data: {
        "model": model,
        "f_key": f_key,
        "pagetitle": pagetitle,
        "keywords": keywords,
        "description": description,
        "niceurl": niceurl
    },
    dataType: 'json',
    complete: function(){
        return false;
    },
    success: function(result) {
        if(typeof result =='object') {
            $('#modal-spinner-seo-update').hide('slow');
            jQuery.each(result, function(field, message) {
                $('#seo-'+field).next('div.error-message').html(message).fadeIn('fast');
            });
        } else {
            $('#modal-spinner-seo-update').hide('slow', function() {
                $("#seo-widget-message-success").fadeIn('slow').delay(2000).fadeOut('slow');
            });
        }
        return false;
    }
});
Community
  • 1
  • 1

1 Answers1

0

Something else to think about, in addition to the dataType:

Since it's returning a 403 error, have you added the 'saveMetas' method in the $this->Auth->allow() method in the beforeFilter() of 'MetasController' of your CakePHP project?

class MetasController extends AppController {
    public function beforeFilter() {
      parent::beforeFilter();
      $this->Auth->allow('saveMetas');
    }
    ...
    ...
}

EDIT:

Since you said you have done this, do you have $this->autoRender = false; and $this->layout = 'ajax'; as well in your saveMetas function?

Lastly, since you can visit that page directly, do a pr( $this->request ) after the initial function call and visit the page without AJAX to see what it is telling you. 403 forbidden tells me it's a permissions issue.

J.R.
  • 146
  • 8
  • Yes I have done this ... I think if I didn't the XMLHttpRequest wont work either ... – Rathes Sachchithananthan Oct 09 '14 at 12:34
  • Great I got the mistake: Class 'User' not found in /[...]/Controller/MetasController.php on line 58 But there is a User class ... – Rathes Sachchithananthan Oct 09 '14 at 12:47
  • Did you add the Users class to your model (via a relationship) or via App::uses() in your MetasController? – J.R. Oct 09 '14 at 12:50
  • I have User.php in my Model with a static function get() and then in line 58 I call User::get() – Rathes Sachchithananthan Oct 09 '14 at 12:55
  • Did you load the User model in your MetasController via `$this->loadModel('User')`? – J.R. Oct 09 '14 at 12:57
  • Yeees, you done it! Thank you very much. But could you please tell me why it did work on my localhost? And why did the XMLHttpRequest Version work? – Rathes Sachchithananthan Oct 09 '14 at 13:05
  • I'm guessing on your localhost you either were logged in or don't have to log into the app, or that you have an App::uses() declaration at the top of your controller. To be really sure I would have to see all your Controller and Model code for those two modules. Otherwise, I'm just guessing. – J.R. Oct 09 '14 at 13:25