K
K
Kifw2017-05-09 03:23:04
PHP
Kifw, 2017-05-09 03:23:04

Ajax. How to get an array from a handler?

There is a js file

function auth(){ 
  var msg=$('.form_auth').serialize();
  $.ajax({
    type:'POST',
    url:"ajax/handler.php",
    data:msg,
    cache:false,
    success:function(data){
      $("#error").html(data);
    }
  });
}

As I understand it, js receives from the handler what is displayed in echo or print_r.
Is it possible to return an array, something like return?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Serezha, 2017-05-09
@Kifw

How difficult it is to explain.
Gets the body of the document.
Are you asking how to give php a json data handler?
Let's say we have an array

$arr = [
"name" => "vasya", 
"age" => "23"
];

Using the json_encode function, we output this array as json.
PS If I still do not understand the question, then leave a comment with explanations.

E
entermix, 2017-05-09
@entermix

The server side handler can only return a string, but you can use json_encode to convert a PHP array into a JSON representation and then process it on the JS side
die(json_encode($array));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question