in the following code, why does it work:
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
but
var addresses = json.val;
does not work
my json output is valid!
{"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"}
the error i get is
a is undefined [Break on this error] a))();else c.error("Invalid JSON: "+a)...f(d)if(i)for(f in a){if(b.apply(a[f],
<script>
$(function() {
function log( message ) {
$( "<div/>" ).text( message ).prependTo( "#log" );
$( "#log" 开发者_如何学运维).attr( "scrollTop", 0 );
}
$( "#companies" ).autocomplete({
source: ";companies",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
$("#address").html(ui.item.id);
$.ajax({
type: 'GET',
url: ';addresses?company=' + ui.item.id,
dataType: 'json',
// process the addresses
success: function(json) {
$('body').append('Response Value: ' + json.val);
var opts = '';
var addresses = {"2-avenue-bir-hakiem": "2 Avenue Bir Hakiem", "56-rue-marcel-pagnol": "56 rue Marcel Pagnol"};
//var addresses = json.val;
$.each(addresses, function(k, v) {
opts += '<option>' + v + '</option>';
});
$('#address').html(opts);
}
}); //end ajax
} // end select
});
});
</script>
what am i missing?
thanks
jQuery.parseJSON(response);
Try this before $.each
, cause Jquery thinks that your answer is a string, not a JSON-object.
It looks like you're not getting valid JSON back from the server, not that the addresses
JSON is invalid (though it's just an object literal). Are you sure this is the right URL?
url: ';addresses?company=' + ui.item.id
Check the response coming back in firebug, chrome, fiddler...whatever you can use to see that response, and check that it's valid here: http://www.jsonlint.com/
It appears from your symptoms that you're currently getting a completely empty response back, and I think the URL you're using is to blame, double check what you're trying to fetch.
精彩评论