Get JSON data from a PHP file to jQuery and put it on selectbox
I have a big problem here. I want to receive data from a PHP file.
I do this:
$("#id_categoria").change(function(){
var id = $(this).val();
$.ajax({
type: "POST",
url: "<?php echo ROOT . '/control/functions.php'; ?>",
data: "action=getsubbycat&id="+id,
success: function(data){
alert(data);
}
});
});
alert(data)
returns [{"id_subcategoria":"1","nome":"Port\u00e1teis"},{"id_subcategoria":"2","nome":"Desktop"}]
.
And how can I consume this? And put in <option value="2">Desktop</option>
?
And why is Portáteis
equal to Port\u00e1teis
? Is it because my database is in UTF-8?开发者_开发技巧
You could use parseJSON to turn the string into a JSON object, then either build an object to insert into DOM using JQuery, or document.write() the desired output.
You should use
var new_data = $.parseJSON(data)
to get the object.
Now you can use object.nome
, etc.
Try
var new_data = $.parseJSON(data);
And when you want nome
, try the following.
alert(new_data[0].nome);
精彩评论