ERROR Uncaught SyntaxError: Unexpected token in JSON at position 0

0

I have the following JS script:

   jQuery(document).ready(function($) {
    $("#idSelect").change(function(event) {
  var valor = $(this).val();
    //alert(valor);
      $.post( "ajaxSerie.php", { valorInput: valor }, function( data ) {
        var retorno = JSON.parse(data);
        console.log(retorno);
        $("#pertence").val(retorno['pertence'])// aqui estou atribuindo um input qualquer o valor retornado do php, o input tera o valor de sala206
        $.each(retorno, function() {
          $('<option>').val(retorno['pertence']).text(retorno['pertence']).appendTo('#teste');
        });
    }); 
}); 

});

In AJAX it looks like this:

  $idValor = $_POST['valorInput']; 
  $result = [
  "pertence" => $idValor
  ];
  echo json_encode($result);

When I do site it works perfectly, now when I step to the site on the seuginte server error: Uncaught SyntaxError: Unexpected token < in JSON at position 0, how to solve this?

    
asked by anonymous 13.12.2018 / 18:02

1 answer

0

In fact, it is not a php error, but rather a CORS that in general terms means that you are making a call from one server to another, without owning the other (being in the same domain). >

If you have access to the other server (what you are calling), add this policy in .htaccess

 header("Access-Control-Allow-Origin: *");
    
13.12.2018 / 18:54