donglao7947 2014-10-09 12:23
浏览 63
已采纳

jQuery AJAX与XMLHttpRequest

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;
    }
});
  • 写回答

1条回答 默认 最新

  • doubiao1734 2014-10-09 12:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试