jQuery serialize () method not working

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 does not work.

Below 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>

AJAX call:

$('#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();
});
    
asked by anonymous 02.12.2014 / 13:20

1 answer

0

Possibly the problem is with the Java code because I changed the name's to demonstrate the operation of serialize ();

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

obs: I tested in my Java environment and the name's were dynamically assigned  allowing serialize (), check if it is occurring this way 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><!--ModaldeDestinos--><divclass="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>
    
11.04.2018 / 04:18