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 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)