从表单中检索的Ajax序列化数据中设置JSON数据

时间:2022-12-04 15:25:00

This is my html form:

这是我的html表单:

<form role="form" id="booking" class="form-horizontal" action="booking.json" method="POST">
                <div class="form-group">
                    <label for="destination">Destination:</label>
                    <select id="destination" name="destination" class="form-control" title="Select your Destination...">
                        <option value="Dominican Republic">dominican</option>
                        <option value="Miami, Florida">miami</option>
                        <option value="Las Vegas, Nevada">vegas</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="quantity">Nights:</label>
                    <input type="number" id="quantity" name="quantity" value="1">
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-default text-uppercase">Book Trip</button>
                </div>
                <div class="form-submitted" style="display: none">
                </div>
            </form>

This is my Ajax:

这是我的Ajax:

$.ajax( form.attr('action'), {
        timeout: 3000,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: form.serialize(),
        beforeSend: function () {
            $('.booking-flight').addClass("spinner");
        },
        complete: function () {
            $('.booking-flight').removeClass("spinner");
        },
        error: function (request, errorType, errorMessage) {
            alert(errorMessage);
        },
        success: function (response) {
            console.log($('form').serialize());
            form.remove();
            var msg = $("<p></p>");
            msg.append("Destination: " + response.destination);
            msg.append("Nights: " + response.quantity);
            $('.booking-flight').hide().html(msg).fadeIn();
        }
    });

This is my JSON File with random information that I set:

这是我的JSON文件,其中包含我设置的随机信息:

{
  "destination": "Dominican Republic",
  "totalPrice": 199.99,
  "quantity": 5,
  "confirmation": 234235
};

How can I retrieve the serialized data that I send from my Ajax and set it to my JSON's File... So that I got the correct data from my form?

如何检索我从Ajax发送的序列化数据并将其设置为我的JSON文件...这样我从表单中获取了正确的数据?

2 个解决方案

#1


You can use the jQuery function .serializeArray(); and then manipulate the response as described here: https://*.com/a/1186309/507384

你可以使用jQuery函数.serializeArray();然后按照此处所述操作响应:https://*.com/a/1186309/507384

#2


You can use json_decode($user_json_data)

你可以使用json_decode($ user_json_data)

json_decode($user_json_data) would return a object
and
json_decode($user_json_data,TRUE) would return a array

json_decode($ user_json_data)将返回一个对象,json_decode($ user_json_data,TRUE)将返回一个数组

#1


You can use the jQuery function .serializeArray(); and then manipulate the response as described here: https://*.com/a/1186309/507384

你可以使用jQuery函数.serializeArray();然后按照此处所述操作响应:https://*.com/a/1186309/507384

#2


You can use json_decode($user_json_data)

你可以使用json_decode($ user_json_data)

json_decode($user_json_data) would return a object
and
json_decode($user_json_data,TRUE) would return a array

json_decode($ user_json_data)将返回一个对象,json_decode($ user_json_data,TRUE)将返回一个数组