I am in the process of creating a client-server application where I send a request from the client to the server using a JSON object for registration. However, despite receiving a JSON response with an "OK" field as expected, the client keeps triggering the .fail function instead of the .done one (I apologize if my terminology is not entirely accurate, as I am new to this).
Below is the code I have written for reference:
Client-side JavaScript:
define(['ojs/ojcore', 'knockout', 'jquery', 'appController', 'jquery', 'ojs/ojknockout', 'ojs/ojinputtext'],
function(oj, ko, $, app) {
function RegistrarseViewModel() {
var self = this;
this.email = ko.observable();
this.pwd1 = ko.observable();
this.pwd2 = ko.observable();
this.registrar = function(){
alert("Registration request sent");
var p = {tipo:"Registrarse", email: this.email(), pwd1:this.pwd1(), pwd2:this.pwd2()};
$.ajax({
type: "POST",
url: "http://localhost:8080/ServidorWeb/Registrarse.jsp",
data: "p=" + JSON.stringify(p)
}).done(function(data, textStatus, jqXHR){
alert("Checking type");
if (data.tipo == "OK"){
sessionStorage.jugador = self.email();
app.router.go("login");
alert("Registration successful");
} else {
alert(respuesta.texto);
}
}).fail(function() {
alert("Sorry. Server unavailable.");
});
}
this.cancelar = function(){
app.router.go("login");
}
}
return new RegistrarseViewModel();
}
);
Server-side JSP:
<%@ page language="java" contentType="application/json ; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import= "org.json.*,dominio.Manager"%>
<%
String p = request.getParameter("p");
JSONObject resultado=new JSONObject();
try{
JSONObject jso= new JSONObject(p);
if(!jso.getString("tipo").equals("Registrarse")){
resultado.put("tipo","NOK");
resultado.put("texto","Unexpected message");
}else{
String email=jso.getString("email");
String pwd1=jso.getString("pwd1");
String pwd2=jso.getString("pwd2");
Manager.get().registrarse(email,pwd1,pwd2);
resultado.put("tipo","OK");
resultado.put("texto","You have registered with the email " + email);
}
}
catch(Exception e){
resultado.put("tipo","NOK");
resultado.put("texto","Unexpected message");
}
%>
<%=resultado.toString()%>
After executing Manager.get().registrarse(email,pwd1,pwd2); (which handles the registration logic in MongoDB), it proceeds with the resultado.put("tipo","OK"); line, indicating that the issue does not lie within this area.
In addition, when I make a request like http://localhost:8080/ServidorWeb/Registrarse.jsp?p=%7Btipo:%22Registrarse%22,email:%2233%22,pwd1:%2220%22,pwd2:%2220%22%7D from a browser such as Google Chrome, it returns {"texto":"You have registered with the email 33","tipo":"OK"}, but for some reason, the .done function is not triggered on the actual client side.
I sincerely hope someone can offer assistance on this matter.
Thank you in advance.
EDIT 1: Added the server response from the browser console IMAGE