retrobrain
retrobrain

Reputation: 390

Jquery Mobile getJSON callback not triggered

I am pulling my hair out with this. I am trying to invoke a getJSON and the callback function is never triggered. I tried adding an alert as well as firebug debug but the callback never happens.

The service response shows a JSON string so not sure what is going wrong. Any pointers?

Here is my code which calls a Play Framework service:

            var serviceURL = "http://localhost:9000/signup/";

    var employees;

    $('#employeeListPage').bind('pageinit', function(event) {
        $.mobile.allowCrossDomainPages = true;
        getEmployeeList();
    });

    function getEmployeeList() {
        $.getJSON(serviceURL + 'getemployees?callback=?', function(data) {
            alert(data);
            $('#employeeList li').remove();
            employees = data.items;
            $.each(employees, function(index, employee) {
                $('#employeeList').append('<li><a href="employeedetails.html?id=' + employee.id + '">' +
                        '<img src="pics/' + employee.picture + '"/>' +
                        '<h4>' + employee.firstName + ' ' + employee.lastName + '</h4>' +
                        '<p>' + employee.title + '</p>' +
                        '<span class="ui-li-count">' + employee.reportCount + '</span></a></li>');
            });
            $('#employeeList').listview('refresh');
        });
    }

The play service returns a json string.

Anyone see what am i doing wrong? why is the alert never executed?

Upvotes: 2

Views: 1831

Answers (1)

Dan Blows
Dan Blows

Reputation: 21174

Some debugging tips:

  1. Ensure that getEmployeeList() is being called. Put a console.log('getEmployeeList()') command right at the top of that function. If it's not being called, then my guess would be that #employeeListPage does not exist (at least, not when you are expecting it to.)
  2. Check the URL that you are attempting to call is what you think it is. Log the whole URL.
  3. Use Firebug to look at the requests and responses. Can you see the page being requested? Does it give a valid response? If not, then your success callback function will not be called. Add a failure callback as well.

EDIT Based on comments - seems that the Play framework does not natively support JSONP. I'm not sure whether you have any control over that. http://groups.google.com/group/play-framework/browse_thread/thread/253107904a5c98f8

Upvotes: 1

Related Questions