简体   繁体   中英

jQuery.ajax to an html file with utf-8

I'm trying to pass a String to my controller, using jQuery.ajax() . It does his job, but when I receive a certain String (provided by the user), it has a bad encoding, it has an à instead an á , for example.

The String it still OK when I get it from the form, until I send it with jQuery to the controller.

This is the JS function I am using:

function sendMail(accion, idConflicto, idIncidencia, idUsuario, dia){
    var params = {};
    params["accion"] = "enviar_mail";
    params["idConflicto"] = idConflicto;
    params["idIncidencia"] = idIncidencia;
    params["idUsuario"] = idUsuario;
    params["dia"] = dia;
    params["mensaje"] = jQuery('#idCuerpoCorreo').val();

    jQuery.ajax({
        url: "recargarConflicto.html",
        dataType: "html",
        data: params,
        cache: false,
        async: false,
        success: function(model) {
            jQuery('#dialog-notificar-usuario').dialog('close');
            dialogMensaje_abrir("Correo electrónico enviado correctamente.");
        },
        error: function() {
            dialogMensaje_abrir("Error al enviar el correo.");
        }
    }); 
}

And this is the function in my controller that receives the String and sends the email.

@RequestMapping(value = URI_RECARGAR_CONFLICTO)
public ModelAndView recargarConflictoModificado(@RequestParam(value = "accion") final String accion,
        @RequestParam(value = "idConflicto") final Long idConflicto,
        @RequestParam(value = "idIncidencia") final Long idIncidencia,
        @RequestParam(value = "idUsuario") final Long idUsuario,
        @RequestParam(value = "dia") @DateTimeFormat(pattern = "dd/MM/yyyy") final Date dia,
        @RequestParam(value = "mensaje") final String mensaje) {

    final ModelAndView mv = new ModelAndView(getFormView());

    if(ACCION_ENVIAR_MAIL.equals(accion)){
        enviarMail(idUsuario, mensaje);
    }
.
.
.

private void enviarMail(Long idUsuario, String cuerpo){
    String dni = personalService.findById(idUsuario).getDocIdent();
    String email = personalWlfService.findByNIF(dni).getEmail();
    String asunto = "[TRAMA] Anomalía en los marcajes";

    correoHelper.enviaEmail(email, asunto, cuerpo, null);

}

The param "cuerpo" is the one that should be correctly in utf-8, but it doesn't.

Any suggestions regarding what can be wrong?

You should encode your data, something like:

params["idConflicto"] = encodeURIComponent(idConflicto);

This should be done for all your params.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM