Return of JSON Data

Asked

Viewed 59 times

0

I am performing a simple data return in a Jquery function. However, the error message is displayed. Follows code

JSON:

<?php 
                 header('Content-Type: application/json');
                 $dados = "Meus Dados";
                 echo json_encode($dados, JSON_PRETTY_PRINT);     
?>

Jquery:

function carregarJson(){
 $.ajax({
      url: "json.php",
      dataType: 'json',
      success: function(data){
        alert(data);   
      },
      error:function(){
        alert("erro");
      }   
    }); 

  }
  • What an error message?

  • only that "error" Alert is displayed

  • Of the one console.log(data) in error:function ...

  • Try this format: header('Content-Type:' . "text/plain");

1 answer

1

Simple function for data return with Json

JSON:

<?php 
                 $dados = "Meus Dados";
                 echo json_encode($dados, JSON_PRETTY_PRINT);     
?>

Jquery:

$.ajax({
      url: "inc/json.php",
      dataType: 'json',
      contentType: "application/json; charset=utf-8",
      success: function(data){
        alert(data);   
      },
      error:function(){
        console.log(data);
      }   
    }); 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.