using jquery ajax to load info from database

こ雲淡風輕ζ 提交于 2019-11-29 12:08:26

Try:

var grbData = $.ajax({
        type : "GET",
        url : "http://grb.sonoma.edu:81/getgrbs.php",
        data : "start=0&perPage=3",
        success: function (html) {
            $("#ajaxDiv").html(html);
        }
});

The reason it isn't working is that it's trying to use your html before it's finished loading. The code is executing faster than the result is returned.

To keep your click event, you can use .live so it will fire the event for future elements added to the page, like you ajax code.

$(document).ready( function () {
    $(".burst").live('click',function() {
        $(".burst").css("background-color", "");
        $(this).css("background-color", "yellow");
    });
});

If you want to use the first one, you should pass in a async:false.

var grbData = $.ajax({
    type : "GET",
    url : "http://grb.sonoma.edu:81/getgrbs.php",
    async: false,
    data : "start=0&perPage=3"}).responseText;

The $.ajax call is asychronous so the $.ajax call will give you that message you saw about data not being ready. This of course kind of defeats the purpose of AJAX since user interaction is blocked.

A better method might be:

var grbData = $.ajax({
    type : "GET",
    url : "http://grb.sonoma.edu:81/getgrbs.php",
    dataType: 'html',  // assuming your service returns HTML
    success: function(data) {
      $("#ajaxDiv").html(data);
    }
 });

You are most likely having problems because you are trying to access elements of the page before the HTML has been completely received (or parsed) by the browser. Ensure that you have waited for the page to finish loading before trying to modify the page. The first call to your ajax method should probably occur at the onload event, or be triggered by the onload event to happen at a later time.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!