getJSON on FCC data callback not working?
I am getting a successful response back, but I do not fully understand how to loop through the data I get back. I put in an alert in the success callback, but that wasn't called. Here is the markup and script:
<input type="text" name="searchValue" id="searchValue"/>
<input type="button" name="btnGetLicenes" value="Get Licenses" id="btnGetLicenses"开发者_如何学Python/>
<div id="Licenses"></div>
<script>
$.getJSON("http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon Wireless&format=jsonp&callback=?",
function (data) {
$.each(data.License, function (i, lic) {
$('#Licenses').append('<p>' + lic.licName + '</p>');
alert("hello"); //This is not called.
});
});
</script>
In the above scenario, I am not using the search text box, I just hard-coded Verizon Wireless for testing.
You have an intermediary data.Licenses.License
node which represents the collection you can loop through:
$.getJSON('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless&format=jsonp&jsonCallback=?',
function (data) {
$.each(data.Licenses.License, function (i, lic) {
$('#Licenses').append('<p>' + lic.licName + '</p>');
});
}
);
Also $alert
is not a function that you can expect to be called. Maybe you meant alert
.
Also notice that according to the documentation the parameter that allows you to set the JSONP callback name is jsonCallback
, not callback
as in your example.
Also don't forget to URL encode your query string parameters or you might get unexpected/wrong behavior from the server: searchValue=Verizon Wireless
should be searchValue=Verizon+Wireless
.
And finally here's a live demo to see this in action.
精彩评论