簡體   English   中英

$ POST沒有更新

[英]$POST isn't updating

我的腳本在這里遇到問題,我不明白為什么。 經過大量測試后,我了解到主要問題是Ajax請求。 另外,我還在其他版本的腳本中使用了Ajax請求...那時我的內容更改非常順利...但是現在我想啟用后退按鈕,所以我在這里問這將如何工作。 ..我有一個很好的答案,所以我嘗試在本教程的幫助下做到這一點,現在的結果如下:現在更改url的工作原理,舊內容逐漸消失,而新內容(來自請求)沒有更新… 更新 :甚至沒有發送請求…有人可以告訴我原因嗎?

謝謝高級

抱歉我的英語不好。

JavaScript:

$(function () {

    var newHash = "",
        $content = $("#content"),
        $el;

    $('ul li a').click(function () {
        window.location.hash = $(this).attr("href");
        return false;
    });

    $(window).bind('hashchange', function () {

        newHash = window.location.hash.substring(1);
        url=$(this).attr("href");
        if (newHash) {
            $content

            .fadeOut(200, function () {

                //                 url=url.replace('#','');

                $('#loading').css('visibility', 'visible'); //show the rotating gif animation

                $.ajax({
                    type: "POST",
                    url: "load_page.php",
                    data: 'page' + url,
                    success: function (msg) {

                        if (parseInt(msg) != 0) //if no errors
                        {
                            $('#content').html(msg); //load the returned html into pageContet

                        }
                        $('#loading').css('visibility', 'hidden'); //and hide the rotating gif
                    }

                });

                $('ul li a').removeClass("current");
                $("'ul li a'[href='" + newHash + "']").addClass("current");
            });

        };

    });

    $(window).trigger('hashchange');

});

PHP:

<?php
if(empty($_POST['page'])) die("0");

$page = $_POST['page'];

//  validate it as alphanumeric before reading the filesystem
if (preg_match('/^[a-z0-9]+$/i', $_POST['page']) && file_exists('article/'.$page.'.html')) {
  echo file_get_contents('article/'.$page.'.html');
}
else echo 'There is no such page!';
?>

更新:在firebug插件中,我得到了:

錯誤:語法錯誤,無法識別的表達式:'ul li a [href ='anmeldung']

這就是我的FireBug Consol的樣子... 這是否表示網址為空?

更改此行:

$("'ul li a'[href='" + newHash + "']").addClass("current");

進入這一個:

$('ul li a[href="' + newHash + '"]').addClass('current');

至少那是 FireBug抱怨的語法錯誤。

您以錯誤的方式將數據發送到PHP腳本。 您應該以key=value形式發送它,就像keyvalue那樣發送,因此您的PHP腳本永遠不會獲得page變量。

因此,從

data: 'page'+url,

data: 'page='+url,
//         ^
 $(function() {

    var newHash      = "",
        $content = $("#content");



    $('ul li a').click(function() {
        window.location.hash = $(this).attr("href");
        return false;
    }); 

    $(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);
        url=newHash
        if (newHash) {
            $content

                .fadeOut(200, function() {

         url=url.replace('#','');

 $('#loading').css('visibility','visible'); //show the rotating gif animation

   $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        success: function(msg){

               if(parseInt(msg)!=0) //if no errors
            {
                 $content.fadeIn(200, function() {
            $('#content').html(msg);}); //load the returned html into pageContet

            }  $('#loading').css('visibility','hidden');//and hide the rotating gif
        }

    });

                        $('ul li a').removeClass("current");
                        $('ul li a[href="' + newHash + '"]').addClass('current');
                    });

        };

    });

    $(window).trigger('hashchange');

});

啊,我發現了錯誤,問題是我將網址設置為延遲...感謝所有人-看着並幫助我解決了這個問題的機器人^^

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM