如何进行ajax调用,将值成功返回给控制器?

时间:2019-10-29 16:05:12

标签: jquery ajax spring api

几天前我已经问过类似的问题,但是现在我遇到了问题。我无法成功将想要的值返回给控制器。假设我有这个控制器

    @RequestMapping(value = "/ricercaUtente", method = {RequestMethod.POST,RequestMethod.PUT}, consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<BaseAjaxResponse<List<UtenteDto>>> ricercaUtente( @RequestParam (required=false) String nome,                                                         @RequestParam (required=false) String cognome){

返回列表;     }

这是我的html ..

    <form id="myForm" action="/rest/ricercaUtente">
            <input type="text" id="nome" name="nome">
            <input type="text" id="cognome" name="cognome">
            <input type="submit" id="submit" name="submit">
        </form>

这是我的ajax

    $(function() {
console.log( "DOM pagina di esempio caricato" );

$("#myForm").submit( function(evt){
    //Stoppo il comportamento di default del submit
    evt.preventDefault();
    ricercaUtente();
} );

    });


    function ricercaUtente(){
var ricarcaUtenteUrl = $("#ricercaUtenteUrl").val();
nome = $("#nome").val();
cognome = $("#cognome").val();
$.ajax({
    url : ricarcaUtenteUrl,
    dataType : 'json',
    contentType : 'application/json; charset=UTF-8',
    type : 'POST',
    data: JSON.stringify({nome:nome,cognome:cognome}),//tried this and                   {nome: nome, cognome: cognome}

    beforeSend : function(){
        var busyImgUrl = $("#busyImgUrl").val();
        $.blockUI({ message: '<h1><img src="'+busyImgUrl+'" /></h1>' });
    },
    complete   : function(){
        $.unblockUI();
    },
    success : function(data) {
        var codiceEsito = data.operationResultCode;
        var datiContatto = new Object();
        if (codiceEsito == 200) {
            datiContatto.utente = data.payload;
        } else{
            datiContatto.errore=true;
        }

    },
    error : function(data) {
        var datiContatto = new Object();
        datiContatto.errore=true;
    }
});}

我尝试以调试模式启动tomcat,但是值“ nome”和“ cognome”始终为空...有人知道我在做什么错吗?

0 个答案:

没有答案