Serialize() jQuery method not working

Asked

Viewed 1,496 times

0

I have a modal with some fields in which I make an AJAX call for data processing, but the problem is when jQuery will serialize the inputs of my form. It just doesn’t work.

Down with my modal:

<!-- Modal de Destinos-->
<div class="modal fade" id="destinationModal" role="dialog"
    aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">

        <div class="modal-content">
            <form id="destination-modal-form"
                action="./addSelectedDestination" method="post">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                    </button>
                    <h4 class="modal-title" id="myModalLabel">Negociar Novo
                        Destino</h4>
                </div>

                <div class="modal-body">

                    <div id="div-modal-input-body-destination"
                        class="aling-form col-sm-12 nest text"
                        style="padding-top: 25px">

                        <div class="box01">
                            <select id="destination-passenger-list" class="form-control" name="${customerService.serviceItem.destination}">
                                <c:forEach items="${destinationList}" var="destination">
                                    <option value="${destination.idDestination}">${destination.dtName}</option>
                                </c:forEach>
                            </select>
                        </div>

                        <div class="box02">
                            <div class="input-group skin skin-flat">
                                <input id="ckb-requested" type="checkbox" name="${customerService.serviceItem.requestedDestination}" /> <label
                                    id="ckb-label" for="ckb-requested">Destino
                                    Solicitado pelo Passageiro?</label>

                            </div>
                        </div>

                        <div class="box01">
                            <div class="input-group ">
                                <span id="span-departure"
                                    class="input-group-addon btn-success"><i
                                    class="fa fa-calendar"></i></span> <input id="input-departure"
                                    type="text"
                                    name="${customerService.serviceItem.departureDate}"
                                    class="form-control" placeholder="Data de Ida" />
                            </div>
                        </div>

                        <div class="box02">
                            <div class="input-group ">
                                <span id="span-arrive" class="input-group-addon btn-success"><i
                                    class="fa fa-calendar"></i></span> <input id="input-arrive"
                                    type="text"
                                    name="${customerService.serviceItem.arrivalDate}"
                                    class="form-control" placeholder="Data de Volta" />
                            </div>
                        </div>

                        <div class="box01">
                            <select class="form-control" name="${customerService.serviceItem.saleType}">
                                <c:forEach items="${listOfSaleTypes}" var="saleType">
                                    <option value="${saleType.key}">${saleType.value}</option>
                                </c:forEach>
                            </select>
                        </div>

                        <div class="box02">
                            <div class="input-group ">
                                <span
                                    class="input-group-addon btn-success"><i
                                    class="fa fa-money"></i></span> <input id="input-price"
                                    type="text" data-thousands="." data-decimal=","
                                    name="${customerService.serviceItem.valueNegotiated}"
                                    class="form-control" />
                            </div>
                        </div>


                        <div class="box03">
                            <textarea id="destination-observations" name="${customerService.serviceItem.negociationObservations}" rows="3" placeholder="Observações..." class="form-control"
                                style="min-height: 130px;"></textarea>
                        </div>


                        <input type="hidden" name="${_csrf.parameterName}"
                            value="${_csrf.token}" />

                    </div>

                </div>
                <div class="modal-footer clear" style="margin: 0px;">
                    <button type="button" class="btn btn-danger"
                        data-dismiss="modal">Cancelar</button>
                    <button id="button-add-destination" type="submit"
                        class="btn btn-primary">Cadastrar</button>
                </div>
            </form>
        </div>
    </div>
</div>

Call AJAX:

$('#destination-modal-form').submit( function(e){

   var url = "${pageContext.request.contextPath}/auth/addSelectedDestination";
   var formdata = $(this).serialize();

   $.ajax({
    type:'POST',
    data: formdata,
    url: url,
    success: function(jsonData){
        alert("Sucesso!!!.");
    }, 
    error: function(error){
        alert("Erro: Tente Novamente preenchendo todos os campos, ou entre em contato com o administrador do sistema.");
    }
   });
   e.preventDefault();
});
  • Is there an error appearing on the console? What is the result of serialize()? An empty string?

  • @Lucas, there is no error in the console. The only thing that serialize returns me is the parameter inside <input type="hidden" name="${_csrf.parameterName}"&#xA; value="${_csrf.token}" />

  • Place e.preventDefault(); just below the function declaration, above the url variable declaration and test.

  • Did I get this working? The code works well on jsFiddle:http://jsfiddle.net/z1x67dv8/

  • Voce is sure that the "name" attributes of the other "inputs" are receiving the past values? are they not null? Because everything else is correct the problem must be in the parameters passed by java

  • What is your jquery version?

  • Another way to serialize would be to put var data = new Formdata($(this)); test this way

Show 2 more comments

1 answer

0

Maybe the problem is with Java code because I changed the names to demonstrate how serialize works();

  • I changed the button type from "Submit" to "button"
  • I changed the function call from: $('#Destination-modal-form') - to $('#button-add-Destination')
  • I commented on the Ajax code and put an Alert in the serialize().

Obs: I tested in my Java environment and the names were dynamically assigned allowing serialize(), check if this is happening in your code.

$(function(){
	$('#button-add-destination').click( function(e){

	var url = "${pageContext.request.contextPath}/auth/addSelectedDestination";
	var formdata = $('#destination-modal-form').serialize();
	alert(formdata);
	/*
	$.ajax({
		type:'POST',
		data: formdata,
		url: url,
		success: function(jsonData){
		alert("Sucesso!!!.");
		}, 
		error: function(error){
			alert("Erro: Tente Novamente preenchendo todos os campos, ou entre em contato com o administrador do sistema.");
		}
	});
	e.preventDefault();
	*/
	});
	
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Modal de Destinos-->
	<div class="modal fade" id="destinationModal" role="dialog"	aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">

			<div class="modal-content">
				<form id="destination-modal-form"
					action="./addSelectedDestination" method="post">

					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal">
							<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
						</button>
						<h4 class="modal-title" id="myModalLabel">Negociar Novo
							Destino</h4>
					</div>

					<div class="modal-body">

						<div id="div-modal-input-body-destination"
							class="aling-form col-sm-12 nest text"
							style="padding-top: 25px">

							<div class="box01">
								<select id="destination-passenger-list" class="form-control" name="destination">
									<option value="1">A</option>
									<option value="2">B</option>
									<option value="3">C</option>
								</select>
							</div>

							<div class="box02">
								<div class="input-group skin skin-flat">
									<input id="ckb-requested" type="checkbox" name="requestedDestination" /> <label
										id="ckb-label" for="ckb-requested">Destino
										Solicitado pelo Passageiro?</label>

								</div>
							</div>

							<div class="box01">
								<div class="input-group ">
									<span id="span-departure"
										class="input-group-addon btn-success"><i
										class="fa fa-calendar"></i></span> <input id="input-departure"
										type="text"
										name="departureDate"
										class="form-control" placeholder="Data de Ida" />
								</div>
							</div>

							<div class="box02">
								<div class="input-group ">
									<span id="span-arrive" class="input-group-addon btn-success"><i
										class="fa fa-calendar"></i></span> <input id="input-arrive"
										type="text"
										name="arrivalDate"
										class="form-control" placeholder="Data de Volta" />
								</div>
							</div>

							<div class="box01">
								<select class="form-control" name="saleType">
									<option value="1">T</option>
									<option value="2">R</option>
									<option value="3">P</option>
								</select>
							</div>

							<div class="box02">
								<div class="input-group ">
									<span
										class="input-group-addon btn-success"><i
										class="fa fa-money"></i></span> <input id="input-price"
										type="text" data-thousands="." data-decimal=","
										name="valueNegotiated"
										class="form-control" />
								</div>
							</div>


							<div class="box03">
								<textarea id="destination-observations" name="negociationObservations" rows="3" placeholder="Observações..." class="form-control"
									style="min-height: 130px;"></textarea>
							</div>


							<input type="hidden" name="csrf"
								value="EWRT565E6" />

						</div>

					</div>
					<div class="modal-footer clear" style="margin: 0px;">
						<button type="button" class="btn btn-danger"
							data-dismiss="modal">Cancelar</button>
						<button id="button-add-destination" type="button"
							class="btn btn-primary">Cadastrar</button>
					</div>
				</form>
			</div>
		</div>
	</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.