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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图