drjv5597 2010-08-06 10:34
浏览 56
已采纳

如何在ajax中发生错误时重定向到自定义Joomla错误代码页?

This questions is bit specific to joomla.

I have created the custom error page error.php in /template/MY_TEMPLATE/error.php. This file checks for the error codes and displays different message for each http code I set(I check with $this->error->code == '404'). Normally this works good.

Now the question is, how should I handle same thing when I am using ajax? When I do an ajax call if an error occurs, I would get http status code back, and the response is the error.php file output. When I get error I would like to redirect the user to the error.php (custom error page) with proper error codes, which would display the messages accordingly!

Scenario : - I am requesting an ajax call to a component with a task (com_greatsuff), which executes some webservice calls to get list of stuff based on itemcode provided. If the itemcode is not found on webservice it woud return 404 Error.(There are many other custom error codes). If it was not an ajax call, it works good as i raise error(JError::raiseError), and displays custom error page. But on ajax call, it will return http status(404) but i need to redirect from javascript with the proper http header. Thanks, Tanmay

  • 写回答

2条回答 默认 最新

  • douyi6755 2010-08-06 15:17
    关注

    Ok, after thinking of various logics, I applied the following. Some one might be helped.. or let me know if any one has a better solution.

    • My main purpose was to show the user the custom error page based on the error codes (404, 403, 500... )
    • Now when ajax fails and returns back with the specific error code (404, 403, 500..) it also returns me the complete custom error page, as discussed in my question.
    • I grabbed the complete HTML page and replaced the current page with it.

    eg:

    $.ajax({
      url: 'index.php?option=com_test&task=listdata&no_html=1',
      success: function(data) {
        $('.result').html(data);
        alert('Load was performed.');
      }
      failure: function(data){
         document.write("");
         document.close();
         document.write(data.responseText);
         document.close();
    }
    });
    

    This will basically overwrite the current page with the custom error page generated through joomla.

    Please let me know if I am doing anything wrong.

    Thanks, Tanmay

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部