views:

36

answers:

1

When using: -

$.getJSON("admin.php?format=json", { module: "data", action: "allBusinessUnitsByClientName", clientname : $('#client').val() }, function(json) {
    $.each(json.items, function(i,item){
        alert(i);
    });
});

I get the following error in the Firebug console:-

a is undefined
a))();else c.error("Invalid JSON: "+a)...f(d)if(i)for(f in a){if(b.apply(a[f],

The Json being returned is in the following format: -

{"550":"Test 1","547":"Test 2","549":"Test 3"}
+5  A: 

You're getting this because json.items is undefined here, you just want json (your object being returned, which has no items property), like this:

$.getJSON("admin.php?format=json", { module: "data", action: "allBusinessUnitsByClientName", clientname : $('#client').val() }, function(json) {
    $.each(json, function(i,item){
        alert(i);
    });
});
Nick Craver
Add I see, thanks. Is there a way of validating a Json request? For example, when using $.ajax({}) I could use success or error to check if the call has been successful.
@user275074 - If the JSON itself is invalid the request will silently fail, you can test your JSON output here: http://www.jsonlint.com/ If you want general error problems, timeouts, access denied any error messagr from the server, etc, then yes use the `error` callback. Are those items what you mean?
Nick Craver
Yes, I would like to use an Ajaxloader while the content loads. If I were to use $.ajax() I would use onBeforeLoad (I think).
@user275074 - Or just show the loader right before your `$.ajax()` call. If you want a more global solution check out [`.ajaxStart()`](http://api.jquery.com/ajaxStart/) and [`.ajaxStop()`](http://api.jquery.com/ajaxStop/) which fire when the first request starts and the last finishes (if you have multiple at once, works equally well for a single) - something like this: `$("#loader").ajaxStart(function() { $(this).fadeIn(); }).ajaxStop(function() { $(this).fadeOut(); });`, then you need nothing at all in each `$.ajax()` call :)
Nick Craver