使用带有参数的jQuery的AJAX的GET请求,返回的页面内容 [英] Using jQuery's ajax get request with parameters, returning page content

查看:93
本文介绍了使用带有参数的jQuery的AJAX的GET请求,返回的页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您看我的问题,因为我AP preciate你的时间。

Thank you for looking at my question, as I appreciate your time.

好了,所以我想使用jQuery的get函数来调用我的PHP脚本,最终返回一个变量,它是我的网页的主要内容减去我的静态页眉/页脚的内置模板,为此,我愿更换无需刷新页面的老字号的网页内容。任何人都可以点我在正确的方向,以我要去哪里错了吗?我的code是如下...

Okay, so I'm trying to use jQuery's get function to call my php script which ultimately returns a variable which is a built template of the main content of my page minus my static header/footer, for which I would like to replace the "old" page content without the page reloading. Can anyone point me in the right direction as to where I'm going wrong here? My code is as follows...

jQuery的:

function getData(time, type) {
     $.ajax({
            type: "GET",
            url: "getData.php",
            data: "time=" + time + "&type=" + type,
            success: function(data){
               $('#pageContent').fadeOut("slow");
               $('#pageContent').html(data);
               $('#pageContent').fadeIn("slow");
     }
    });
    return false;
}

getData.php(转述):

getData.php(paraphrased):

    ....

    $time = empty($_GET['time']) ? '' : $_GET['time'];
    $type = empty($_GET['type']) ? '' : $_GET['type'];
    echo getData($time, $type);

    function getData($time, $type)
    ......
        .....
            $tmpl = new Template();
            $tmpl->time= $time;
            $tmpl->type = $type;
            $builtpage = $tmpl->build('index.tmpl');
            return $builtpage;
        .....
    ......

jQuery函数调用:

jQuery function call:

<a href="#" onclick="getData('<?php print $tmpl->time; ?>', 'Orange')">Orange</a>
<a href="#" onclick="getData('<?php print $tmpl->time; ?>', 'Apple')">Apple</a>
<a href="#" onclick="getData('<?php print $tmpl->time; ?>', 'Banana')">Banana</a>

当我点击任何链接,阿贾克斯似乎运行正常,页面也拒绝重装,但内容仍保持不变...任何人都知道发生了什么事?

When I click any link, the ajax seems to run fine, and the page does refuse to reload, but the content still remains unchanged... Anyone happen to know what's up?

推荐答案

首先在成功函数,检查以确保您收到你在找什么:

First in the success function, check to make sure that you are receiving what you are looking for:

success: function(data){
  alert(data);
}

此外,在PHP文件,可以尝试在此对脚本的顶部:

Also in the php file, try putting this on top of the script:

header("Content-Type: text/html");

和尝试修改您的code这样的:

And try modifying your code like:

success: function(data){
  $('#pageContent').html(''); // remove what was before
  $('#pageContent').fadeOut("slow");
  $('#pageContent').html(data);
  $('#pageContent').fadeIn("slow");
}

这篇关于使用带有参数的jQuery的AJAX的GET请求,返回的页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆