1

I am creating a json object in which I am pulling fields from form and then using jquery Ajax POST to send the data. But when I see my network tab after pressing submit I basically get the json headers but all the values that should have been pulled from the form are blank except the values I am hardcoding. Note that my json data also has a nested json of type room.

Below is my jquery part:-

 var formData={
        "checkInDate": $("#checkInDate").val(),
        "checkOutDate": $("#checkOutDate").val(),
        "roomsWanted":$("#roomsWanted").val(),
        "room":{
            roomType: $("input[name=roomType]:checked").val(),
            roomProperty:"non-smoking"
        }

    };
$("#checkAvailabilityForm").submit(function(e){
        e.preventDefault();

        $.ajax({
            type: 'post',
            url: '',
            dataType: 'json',
            data: JSON.stringify(formData),
            contentType: 'application/json',
            success: function(dataRecieved){
                var dataRecieved= $.trim(dataRecieved);
                if(dataRecieved === ''){

                }else{

                }
            }

        });
    });

2 Answers 2

1

Move your declaration of formData inside of the .submit() function. The way you have it now the page loads, and then var formData = ... immediately sets the value for formData (to the values of the new empty form).

Your code should look like this:

$("#checkAvailabilityForm").submit(function(e){
    e.preventDefault();

    var formData={
        "checkInDate": $("#checkInDate").val(),
        "checkOutDate": $("#checkOutDate").val(),
        "roomsWanted":$("#roomsWanted").val(),
        "room":{
            roomType: $("input[name=roomType]:checked").val(),
            roomProperty:"non-smoking"
        }
    };

    $.ajax({
        type: 'post',
        url: '',
        dataType: 'json',
        data: JSON.stringify(formData),
        contentType: 'application/json',
        success: function(dataRecieved){
            var dataRecieved= $.trim(dataRecieved);
            if(dataRecieved === ''){

            }else{

            }
        }

    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot. such a simple error. had been scratching my head for almost an hour.
0

You don't need to stringify your json just past the json as it

data: formData

2 Comments

I had done that but it doesnt show anything and infact doesnt represent it as json as well. This is how I see it in developer console of the browser and the values coming from form are still blank. Looks like this "checkInDate=&checkOutDate=&roomsWanted=&room%5BroomProperty%5D=non-smoking"
actually you were right too because i had the var declared outside submit as suggested by Dark Lord above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.