weixin_33705053 2016-05-15 06:38 采纳率: 0%
浏览 19

GET Ajax方法的问题

I started to work with ajax some time ago, but I'm getting a strange problem that I don't have any idea of the reason.

I work with 3 files to make all my pages: a header, a footer and the content files. In header I put my ajax code that is something like this:

        $(document).on("click", '.ajax a', function (e) {
           e.preventDefault();
           var href = $(this).attr("href");
           if (href == location.pathname) return; // Prevent from refresh on click the current page link
           loadContent(href); // Make the AJAX call
           window.history.pushState('', '', href); // Changes the link
        });

        //MAKE BACK/FORWARD WORKS
        window.onpopstate = function(event) {
           loadContent(location.pathname);
        };

  ///////////////////////////////////////AJAX ITSELF
  function loadContent(url){
     $.ajax({
        url:url,
        type:'GET',
        error: function(){
           alert("Oops, something went wrong :(");
        },
        success:
        function(data){
           $('#content').html(data); // Inject page into the content div
           document.title = $("#titulosads").val(); // Changes the page title
        }
     });

  }

After that I open the #content div that will be closed in footer.

And in each page file, I use these codes for include the header.php and the footer.php when is needed:

function includeheader($pagename, $pagedescription)
{
    $title = $pagename;
    $description = $pagedescription;
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        echo "<input type='hidden' id='titulosads' value='".$title."' />";
    } else {
        include 'header.php';
    }
}

And for the footer:

function includefooter()
{
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    } else {
        include 'footer.php';
    }
}

So, basically, this is my pages:

<?php
include_once 'includes/checkajax.php';
includeheader('Contact');
?>

Content of the page here!

<?php
includefooter();
?>

Well, this is my issue: it gets a little strange when I use the GET method of AJAX.

Things get duplicated when you click in other link and back on the first. You can check it on http://feely.com.br

When I change the method to POST or when I activate chrome dev tools that disable the cache, everything works fine.

Oh, there is a lot of errors on console that I don't have idea about what they mean.

A picture of the errors:

Duplicated content

Help :(

  • 写回答

2条回答 默认 最新

  • weixin_33713350 2016-05-15 06:48
    关注

    I think you was loading a whole pagecontent inside a part of a similar page. So, instead of paste the content in div#content you have to paste it over the whole page. Give it a try.

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题