0
I’m using PHP with Codeigniter, and is giving this error in an ajax I’m using, I can’t figure out what it is, I get these two messages:
syntaxerror: Unexpected token < in JSON at position 1,
And this:
Occurred problem: parsererror Description: Syntaxerror: JSON.parse: Unexpected Character at line 2 column 1 of the JSON data
Whenever I make this request with ajax. My javascript is like this:
if (!erro_validacao) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'http://localhost/painel/pagar/pg_boleto',
data: form.serialize(),
dataType: 'JSON',
beforeSend: function(){
$('.msg_envio_pagamento').html('<div class="alert alert-success" role="alert">Enviando dados de pagamento, aguarde...</div>');
},
success: function(res){
$('.msg_envio_pagamento').html('');
if (res.erro == 0) {
$('.pedido_pagamento').remove();
var msg_sucesso = '<div class="row margin-top-40">'+
'<div class="col-md-12 text-center">'+
'<h2>' + res.msg + '</h2>'+
'<p>Status do pedido: ' + res.status + '</p>'+
'<p>Número do pedido: ' + res.numero_pedido + '</p>'+
'<p>Código da transação: ' + res.cod + '</p>'+
'<p>Código da transação: ' + res.cod + '</p>'+
'<p>'+
'<a href="' + res.url_boleto + '" title="Imprimir Boleto" class="btn btn-success btn-lg" target="_Blank">'+
'Imprimir Boleto'+
'</a>'+
'</p>'+
'</div>'+
'</div>';
$('.pedido_pagamento').html(msg_sucesso);
} else {
$('.msg_envio_pagamento').html('');
$('.erro_validacao').html('<div class="alert alert-danger" role="alert">'+ res.msg +'</div>');
}
},
error: function(request, status, erro, response){
$('.msg_envio_pagamento').html('');
alert('Erro ao salvar cadastro');
alert("Problema ocorrido: " + status + "\nDescição: " + erro);
//Abaixo está listando os header do conteudo que você requisitou, só para confirmar se você setou os header e dataType corretos
alert("Informações da requisição: \n" + request.getAllResponseHeaders());
console.log('response:', response);
}
});
} else {
$('.msg_envio_pagamento').html('');
$('.erro_validacao').html('<div class="alert alert-danger" role="alert">'+ retorno_erro_validacao +'</div>');
}
});
}
And on my Alert request:
alert("Informações da requisição: \n" + request.getAllResponseHeaders());
He returns the answer:
Informações da requisição:
cache-control: no-store, no-cache, must-revalidate
connection: Keep-Alive
content-length: 605
content-type: text/html; charset=UTF-8
date: Sun, 29 Mar 2020 01:12:11 GMT
expires: Thu, 19 Nov 1981 08:52:00 GMT
keep-alive: timeout=5, max=100
pragma: no-cache
server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.4.1
x-powered-by: PHP/7.4.1
I don’t know how else to solve, and the why of returning this, being that in the table of the database, it arrives to write the data, but in the page it returns it.
This fragment is not related to the error reported. The error is being generated in a method call
JSON.parse()
that this fragment does not appear.– Augusto Vasques
Your request is returning
content-type: text/html
, should returncontent-type: application/json
. Perhaps the mistake is not only that, but surely it is wrong.– Rafael Tavares
Mds and how do I see this error? or switch to application/json? I’m a beginner and I bought a small course to learn more, so there are some mistakes that are driving me crazy already, if you can help me thank you
– Ragnar
Gives a Ctrl+f , or equivalent, at its source to locate calls to the method
JSON.parse()
. Surely one of the calls and the problematic, then associate a break point to each call so you can inspect the data passed to the method.– Augusto Vasques
I tried to do, I put a break point, and now it returns only this error: 'Problem occurred: parsererror. Description: Syntaxerror: JSON.parse: Unexpected Character at line 2 column 1 of the JSON data'
– Ragnar
I think there may be some error in the function I am doing. Some more character is being inserted.
– Ragnar
Well after searching so much, it seems that the error is coming from the pagseguro API that I am trying to use, unfortunately I do not know yet pq, but I will try to find out and put here
– Ragnar
@Ragnar, in this breakpoint Specione the string value
data
before being analysed byJSON.parse()
pass it by a JSON validator. PS: put a@
before the name of the user you want to direct the comment otherwise the system does not inform about the communication.– Augusto Vasques
@Augusto Vasques, thanks for the tips brother, for what I got figured out the problem is in my function, he’s passing the empty array, so he does not identify the data, I’ll have to ask for help in another forum, because my head is already boiling kkkk mto thanks for the help. Valew
– Ragnar