1

How can I get the values that I'm setting in my controller and set in my td descriptions with Jquery Ajax.

JQuery function:

jQuery.noConflict();
function preencheDocumento(){

var value1 = jQuery("#nroDocumento").val();

jQuery.ajax({
    type: "get",
    url: "obterDocumento.do",
    data: 'nroDocumento=' + value1,
    success: function(extratoLancamento) {
        $(form.extratoLancamento.achouLancamento).val(extratoLancamento.achouLancamento);
        $(form.extratoLancamento.data).val(extratoLancamento.data);
    },
    error: function() {
        alert("Erro ao obter esse documento!");
    }
});    
}

Spring Controller MVC where I fill the object:

@RequestMapping(value="/obterDocumento.do", method=RequestMethod.GET)
    public @ResponseBody ExtratoLancamento obterDocumento(Long nroDocumento) {
        ExtratoLancamento extratoLancamento = new ExtratoLancamento();
    try {

        extratoLancamento.setAchouLancamento(true);
        extratoLancamento.setData("16/06/2017");
        extratoLancamento.setDebito("100");
        extratoLancamento.setCredito("200");
        extratoLancamento.setNroDocumento(nroDocumento);
        extratoLancamento.setHistorico("Teste");
        extratoLancamento.setSaldo("500,2");

    } catch (Exception e) {
        e.printStackTrace();
    }

    return extratoLancamento;
} 

Jsp Page where i wanna the values filled

<c:choose>
                                <c:when test="${form.extratoLancamento.achouLancamento}">                 
                                      <tr>
                                         <td class="alignCenter" colspan="3">
                                          <input type="hidden" name="numDocPesq" id="numDocPesq" />
                                          <table class="listagem">
                                           <thead>
                                            <tr>
                                              <td width="10%" class="alignCenter">Data</td>
                                              <td width="10%" class="alignCenter">N&ordm; Doc.</td>
                                              <td width="50%" class="alignCenter">Histórico</td>
                                              <td width="10%" class="alignRight">Débito</td>
                                              <td width="10%" class="alignRight">Crédito</td>
                                              <td width="10%" class="alignRight">Saldo</td>
                                            </tr>
                                           </thead>
                                           <tbody>
                                             <tr class="print">
                                               <td class="alignCenter" name="data">${form.extratoLancamento.data} </td>
                                               <td class="alignCenter" name="numDoc">${form.extratoLancamento.numDoc}</td>
                                               <td name="historico">${form.extratoLancamento.historico} </td>
                                               <td class="alignRight" name="debito">${form.extratoLancamento.debito} &nbsp;</td>
                                               <td class="alignRight" name="credito">${form.extratoLancamento.credito}</td>
                                               <td class="alignRight" name="saldo">${form.extratoLancamento.saldo}</td>
                                             </tr>
                                          </tbody>
                                          </table>
                                          </td>
                                      </tr>
                                </c:when>
                          </c:choose>
  • Most of your code are correct except the `success` function in ajax. You need to find html element to set value to it instead. Your can see the post [here](https://stackoverflow.com/questions/15385435/assigning-html-returned-via-ajax-to-any-html-element-value) to know how to set value to html in ajax return. – TuyenNTA Jun 16 '17 at 22:35
  • I don't get it @TuyenNguyen can you show me how would be? I tried $("#saldo").val(extratoLancamento.saldo) and $("#saldo").text(extratoLancamento.saldo) but did not work – Stenio Vilar Jun 16 '17 at 23:01
  • @Stinio Vilar you don't get it because you are using the wrong way to find html element. `saldo` in html is a `name` but you using `$("#saldo")` the `#` is for finding the `id`. You might need: `$('[name="saldo"]').val(extratoLancamento.saldo)`. You can refer [here](https://stackoverflow.com/questions/9680037/how-do-i-select-an-element-with-its-name-attribute-in-jquery) – TuyenNTA Jun 16 '17 at 23:06
  • @TuyenNguyen I received: Cannot read property 'val' of null using $('[name="saldo"]').val(extratoLancamento.saldo) and I removed – Stenio Vilar Jun 16 '17 at 23:13
  • It's means that the `$('[name="saldo"]')` is null. Please check in your browser by press F12 if is there any element with name = saldo? Because the syntax in script is right. [Check it again](https://www.w3schools.com/jquery/sel_attribute_equal_value.asp) – TuyenNTA Jun 17 '17 at 03:06
  • didn't work, i had to use document.getElementById(“elementID”).innerHTML – Stenio Vilar Jun 19 '17 at 20:19

0 Answers0