Answer the question
In order to leave comments, you need to log in
How to get json data back using ajax?
I'm learning web programming, making a simple easy photo uploader. I took part of the code from the example.
here is the function that sends the data
$.ajax({
url:'add_avatar1.php',
type:'POST',
data: data,
cache: false,
contentType: false,
processData: false,
dataType: "json",
beforeSend: function() {
$('.loading').html('<img src="css/loading.gif" alt="загрузка">');
},
complete:function(data){
$('.loading').html('');
var obj = $.parseJSON(data); // что тут надо написать и как чтоб получить обратно данные из пхп обработчика
alert(obj.name);
console.log(data);
}
});
$x="Переменная";
$arr=array('Аватар успешно загружен', $x);
echo json_encode($arr);
Answer the question
In order to leave comments, you need to log in
On server:
$x = "123";
$arr=array(
"result" => 'success',
"var" => $x
);
header("Content-type: application/json; charset=utf-8");
echo json_encode($arr);
success:function(data){
$('.loading').html('');
console.log(data);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question