php和js-ajax表演搞笑(里面的解释)

时间:2013-03-23 08:45:49

标签: php javascript ajax

我遇到麻烦这个ajax。回报很有趣。开关似乎总是评估#about是否为真。并在#page变量中包含switch语句的其余部分。例如,我的PHP代码打印出这个(以及它应该的#about)底部的代码应该澄清我的意思。

回顾一下,它回显了第一个$ page ='

之后的所有内容

在我的页面上,我在下面看到它应该回应的内容

   ';
           break;

    case '#register' :
           $page = 'k';
           break;

    case '#contact' :
          $page = 'a';
    break;

     case '#a' :
        $page = ' b';
        break;

     case '#b' :
        $page = '<p> c</p>';
        break;


      default;
    echo "def";
   }
   echo $page;

更不用说它甚至不适用于#contact,或#a,#b ......等。我不知道为什么。无论传递的url如何,看起来#about都会被返回的内容调用(即使它返回的内容更多)。

我真的很感激一些帮助!谢谢

这是我的代码:

js =]

$(document).ready(function () {


    //highlight the selected link
    $('a[href=' + document.location.hash + ']').addClass('selected');

    //Seearch for link with REL set to ajax
    $('a[rel=ajax]').click(function () {

        //grab the full url
        var hash = this.href;

        //remove the # value
        hash = hash.replace(/^.*#/, '');

        //clear the selected class and add the class class to the selected link
        $('a[rel=ajax]').removeClass('selected');
        $(this).addClass('selected');

        //hide the content
            $('#content').hide();
        console.log(this.href);
        //run the ajax
        getPage();

        //cancel the anchor tag behaviour
        return false;
    }); 
});


function pageload(hash) {
    //if hash value exists, run the ajax
    if (hash) getPage();    
    console.log("k");
}

function getPage() {

    //generate the parameter for the php script
    var data = 'page=' + encodeURIComponent(document.location.hash);
    $.ajax({
        url: "loader.php",  
        type: "GET",        
        data: data,     
        cache: false,
        success: function (html) {  

            //add the content retrieved from ajax and put it in the #content div
            $('#content').html(html);

            //display the body with fadeIn transition
            $('#content').fadeIn('slow');       
        }       
    });
}

php =]

switch($_GET['page']) {
 case '#about' :
        $page = ' HTML stack overflow formats it, anyways';
        break;

 case '#register' :
        $page = 'k';
        break;

 case '#contact' :
        $page = 'a';
        break;

  case '#a' :
     $page = ' b';
     break;

  case '#b' :
     $page = '<p> c</p>';
     break;


   default;
 echo "def";
}
echo $page;
再次感谢你!

1 个答案:

答案 0 :(得分:0)

同样在php文件中,尝试将其放在脚本之上:

标题(“Content-Type:text / html”);