-1
Good Morning. After 2 full days researching I post my question: Why does the command
echo json_encode($a);
is returning an HTML page ? It is a registration where I have 1 foreign key, but the user will not enter the foreign key ( logical ) but 3 data that will be searched in Mysql to know if the CLASS exists or not. With this, I have to use Javascript.
I am using CODEIGNITER. So on the footer I have the code:
<script type="text/javascript">
$(document).ready(function(){
$("#btgravar").click(function(){
var nturma = $("#nturma").val();
var cletivo = $("#cletivo").val();
var csemestre = $("#csemestre").val();
alert(nturma);
$.ajax({
url: 'Planodeaula/ajaxrequestpost',
type: 'POST',
data: {nturma: nturma, cletivo: cletivo, csemestre:csemestre},
contentType: ('application/x-www-form-urlencoded; charset=UTF-8'),
accepts: {json: "json"},
//dataType: 'json',
success: function(a) {
//console.log(retorno);
alert(a);
//var resposta = jQuery.parseJSON(retorno);
var resposta = JSON.parse(a);
alert(resposta);
if(resposta === 'true')
{
alert("tudo funcionando 100%.");
}else{
alert("NÃO funcionando !!!!!!!!!!!");
}
},
error:function( jqXHR , textStatus, errorThrown )
{
alert("Erro: "+textStatus);
//alert(jqXHR);
alert("Detalhes do erro: "+errorThrown);
}
});
});
});
</script>
If I put the command in the javascript dataType: 'json'; returns that "famous" error "Syntaxerror: Unexpected token < in JSON at position 0"" when removing it returns the whole page in HTML. In Controller ( I invented, after 1,000,000 attempts, a false array to turn into Json ).The right is to go to MODEL this->dia->check_turma(), check if it exists in the database and return to the Controller:
public function ajaxrequestpost()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('nturma','trim|required');
$this->form_validation->set_rules('cletivo','trim|required');
$this->form_validation->set_rules('csemestre','trim|required');
header('Access-Control-Allow-Origin: *');
if ($this->form_validation->run() === FALSE) {
header('Content-type: application/json');
$a = array("tipo"=>$tipo, "rua"=>$rua, "bairro"=>$bairro, "cidade"=>$cidade, "cep"=>$cep, "uf"=>$uf);
$a = utf8_encode($a);
echo json_encode($a);
exit;
//return $a;
}
else{
//show_error('Dados válidos!');
//$retorno=$this->dia->check_turma();
header('Content-type: application/json');
$a = array("tipo"=>$tipo, "rua"=>$rua, "bairro"=>$bairro, "cidade"=>$cidade, "cep"=>$cep, "uf"=>$uf);
$a = utf8_encode($a);
echo json_encode($a);
exit;
}
}
I already tested the Javascript code:
dataType: 'json'
accepts: {json: "json"},
And in PHP : header('Content-type: application/json');
Content-type: application/json; charset=utf-8
header('Access-Control-Allow-Origin: *');
$date = utf8_encode($date);
Exit;
I researched : Request ajax is not working
https://stackoverflow.com/questions/37280274/syntaxerror-unexpected-token-in-json-at-position-0
https://stackoverflow.com/questions/21572721/ajax-call-returning-data-but-failing
Array is not converted to JSON
Why can’t I get the data from the ajax request? (json)
https://makitweb.com/send-ajax-request-codeigniter/
https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script
https://www.itsolutionstuff.com/post/jquery-ajax-request-example-in-codeigniterexample.html
If requested page
ajaxrequestpost
has HTML tags will return tb along with echo’s JSON, and this will result in error. The only thing that should return from the page is what echo prints.– Sam
Thanks I will test. This ajaxrequestpost method is inside a Controller. And in the index of the Controller I have the call to 3 views. Header, side menu_and Footer. Then, I have to take this method out of this Controller and create another Controller to include it. Correct ?
– Rogerio Rios
The requested file cannot be a normal page (with tags and everything). If you need to create a file . php only with PHP code and echo.
– Sam
The function
json_encode()
not the problem, the problem is the data you are passing for it encode, you should not userutf8_encode()
in an array? The parameter for utf8_encode must be string.– Ivan Ferrer
Honestly, I , after 2 days , I put this function utf8_encode(), reading some tips. But the problem is that it returns HTML ( as Sam , I can’t have tags in the file where echo json_encode is). So, I created a . php neat and I’ll insert the Function ( method ) to see the result. Note that the array is "fake", already in the despair of forcing a return a json and not an HTML. I will also follow your hint and remove it.
– Rogerio Rios