PHP - Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON d

 
Vista:

Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON d

Publicado por Sergio Mateo (3 intervenciones) el 14/11/2022 00:48:02
Estimados, buenas tardes:

Me da ese error después de enviar un mensaje con Swit. Lo raro que en el modulo de clientes lo hace bien y en los otros módulos que lo quiero hacer me da ese error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function enviar_formulario_ajax(e) {
  e.preventDefault();
 
  let data = new FormData(this);
  let method = this.getAttribute("method");
  let action = this.getAttribute("action");
  let tipo = this.getAttribute("data-form");
 
  let encabezados = new Headers();
 
  let config = {
    method: method,
    headers: encabezados,
    mode: "cors",
    cache: "no-cache",
    body: data
  };
 
  let texto_alerta;
 
  if (tipo === "save") {
    texto_alerta = "Los datos se guardaran en la base de datos";
  } else if (tipo === "delete") {
    texto_alerta = "Los datos serán eliminados del sistema";
  } else if (tipo === "update") {
    texto_alerta = "Los datos serán actualizados";
  } else if (tipo === "search") {
    texto_alerta =
      "Se eliminara el término de busqueda y tendras que escribir uno nuevo";
  } else if (tipo === "loans") {
    texto_alerta = "Desea remover los datos seleccionados";
  } else {
    texto_alerta = "Quieres realizar la operación solicitada";
  }
 
  Swal.fire({
    title: "Estas seguro",
    text: texto_alerta,
    type: "question",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    cancelButtonColor: "#d33",
    confirmButtonText: "Aceptar",
    cancelButtonText: "Cancelar"
  }).then((result) => {
    if (result.value) {
      fetch(action, config)
        .then((respuesta) => respuesta.json())
        .then((respuesta) => {
          return alertas_ajax(respuesta);
        });
    }
  });
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 393
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON d

Publicado por Jefferson (203 intervenciones) el 14/11/2022 04:22:22
Es muy difícil entender así, pues deberíamos saber que recojes del form y como lo lees en php.

Algo debe estar mal del lado del servidor..

Postea todo completo

Saludos Jefferson Jiménez
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON d

Publicado por Sergio Mateo Cerda (3 intervenciones) el 15/11/2022 02:40:48
Esto es lo que envio desde el php

/*------------- Comprobar que los campos vacios --------*/
if($nombre=="" || $email== "" || $telefono== "" || $direccion=="" ){
$alerta=[
'Alerta' => 'simple',
'Titulo' => 'Ocurrio un error inesperado',
'Texto' => 'No has llenado todos los campos que son obligatorios',
'Tipo' => 'error'
];
echo json_encode($alerta);
exit();
}

Esta es la funcion de javascript
function alertas_ajax(alerta) {
if (alerta.Alerta === "simple") {
Swal.fire({
title: alerta.Titulo,
text: alerta.Texto,
type: alerta.Tipo,
confirmButtonText: "Aceptar",
});
} else if (alerta.Alerta === "recargar") {
Swal.fire({
title: alerta.Titulo,
text: alerta.Texto,
type: alerta.Tipo,
confirmButtonText: "Aceptar",
}).then((result) => {
if (result.value) {
location.reload();
}
});
} else if (alerta.Alerta === "limpiar") {
Swal.fire({
title: alerta.Titulo,
text: alerta.Texto,
type: alerta.Tipo,
confirmButtonText: "Aceptar",
}).then((result) => {
if (result.value) {
document.querySelector(".FormularioAjax").reset();
}
});
} else if (alerta.Alerta === "redireccionar") {
window.location.href = alerta.URL;
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar