javascript - ajax get json function doesn't work

954

Solution:

You must use json_encode() in your php file like this.

<?php
header('Content-type: application/json');

$array = array(
    'firstname' => $_GET['name'],
    'lastname' => 'izadi'
);

echo json_encode($array);

exit;
183

Answer

Solution:

try this cleaner code using json_encode

<?php
header('Content-type: application/json');

$array = array(
    'firstname' => $_GET['name'],
    'lastname' => 'izadi'
);

echo json_encode(array('files'=>array($array)));

js:

 $(document).ready(function(){
   $.getJSON("getfilesinfo.php?name=afshin", function(test){
         alert("Data: " + test.files[1].firstname );
     });  
});

People are also looking for solutions to the problem: php - want to delete a name in database n it must display if the name exists or not

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.