GET响应不能与jQuery一起工作


GET response not working with jQuery

我在我的代码中使用jQuery移动页面结构。我试图在页面onload上使用HTTP GET向服务器上的php文件发送参数,并希望显示我的响应。然而,我没有看到任何回应。下面是我的代码:

<div data-role="page" id="humanities">
    <div data-role="header" style="background-color: #eaffcd;">
        <image src="http://i.cs.hku.hk/~hsbashir/Project_Work/bayview/hku_logo.png" style="height:40px; width:35px; postion:relative; float:left;">
        <h1 style="color:black;"> Departments </h1>
    </div>
    <ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true">
        <li class="departments"><a href="#department">Department of Fine Arts</a></li>
        <li class="departments"><a href="#department">Department of Law </a></li>
    </ul>
</div>
<div data-role="page" id="department" onload="loadCourse()">
    <div data-role="header" style="background-color: #eaffcd;">
        <image src="http://i.cs.hku.hk/~hsbashir/Project_Work/bayview/hku_logo.png" style="height:40px; width:35px; postion:relative; float:left;">
        <h1 style="color:black;"> Courses </h1>
    </div>
    <ul data-role="listview" data-theme="a" data-split-theme="b" data-split-icon="plus" data-inset="true" id="courselist">
    </ul>
    <script>
        function loadCourse(){
        var course = $('.departments').text();
            $.get(
            "getCourses.php?course="+course,
            function( data ){
                $('#courselist').html( data )
                .listview( 'refresh' );
            }
            );      
        }
    </script>
</div>
PHP

mysql_connect($host,$username,$password);
mysql_select_db($database) or die( "Unable to select database");
$course = $_GET['course'];
$query ='SELECT * FROM Course_info WHERE Department="'.$course.'"';
$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)) {
        print '<li><a href="#">';
        print '<h2 style="text-wrap : normal" >'.$row['Course_code'].' '.$row['Title'];
        print '</a></li>';
        print '<p>'.$row['Term'].'</p>';
    }

This:

var course = $('.departments').text;
应:

var course = $('.departments').text(); // missed the braces

当你在其中使用jquery mobile时你可以使用pagecontainershow来检查这里的页面显示我认为这应该是这样的:

$(function(){
  $( ":mobile-pagecontainer" ).on( "pagecontainershow", function( event, ui ) {
    if(window.location.href.indexOf('#department') !== -1){
        loadCourse();
    }
  });
});

ON JSBIN HERE

根据你最近的评论:

这是给我的一切。html

里面的所有文本

您需要获取文本并将其传递给函数:

$(function(){
  var txt; // declare var outside
  $('a').on('click', function(){ // bind a click event on clicked anchors
    txt = this.textContent; // get the text content
  });
  $( ":mobile-pagecontainer" ).on( "pagecontainershow", function( event, ui ) {
    if(window.location.href.indexOf('#department') !== -1){
        loadCourse(txt); // pass it here
    }
  });
});

更新JSBIN

您确定您正在正确处理您的课程变量吗?我认为首先你得把航向变量记录到控制台。然后检查您是否可以处理来自服务器的响应。像这样:

function loadCourse(){
        var course = $('.departments').text();
        console.log(course)
            $.get(
            "getCourses.php?course="+course,
            function( data ){
                console.log(data);
                $('#courselist').html( data )
                .listview( 'refresh' );
            }
            );      
        }  

最好将JavaScript更改为如下内容。从div中删除onload="loadCourse()"。据我所知,后期绑定更好。

<script>
    $(document).ready(function() {
    var course = $('.departments').text();
    $('#sample').html( 'hello' );
        $.get(
        "getCourses.php?course="+course,
        function( data ){
            $('#courselist').html( data )
            .listview( 'refresh' );
        }
        );      
    });
</script>

你只是打了个小错字。

请在函数的line 1上加上括号

var course = $('.departments').text();