繁体   English   中英

如何在ajax中插入数组?

[英]how to insert an array in ajax?

我有模式,在模式内部有一个表单,当我单击“提交”按钮时,它将执行此操作。

jQuery代码:

  $('#add-new-content-form').on('submit', e => { e.preventDefault(); //I want to add this block dates to the data let blockdates = $("#block-dates").val(); let title = $("#card-title").val(); let catalogId = $("#catalog").val(); let categoryId = $("#category").val(); let subcategoryId = $('#subcategory').val(); let why = $("#why").val(); let description = $('#card-description').val(); let cancellationPolicy = $('#cancellation-policy').val(); let displayPrice = $('#display-price').val(); let displayDiscounted = $('#discounted-price').val(); let displayMaxPax = $('#display-maxpax').val(); let data = { "blockDates":[ { "description": "national araw ng mga puso day!", "notAvailableDate": "2019-02-14 10:00:00" }, { "description": "chinese new year!", "notAvailableDate": "2019-02-25 10:00:00" } ], "title": title, "catalogId": catalogId, "categoryId": categoryId, "subcategoryId": subcategoryId, "why": why, "description": description, "cancellationPolicy": cancellationPolicy, "displayPrice": displayPrice, "displayDiscounted": displayDiscounted, "displayMaxPax": displayMaxPax }; let content = ajax("api/unitContents", JSON.stringify(data), "POST"); // window.location.replace("/category"); }); 

现在,在邮递员那里是这样的:

{   
"blockDates":[ 
    { 
        "description": "national araw ng mga puso day!",
        "notAvailableDate": "2019-02-14 10:00:00"
    },
    { 
        "description": "chinese new year!",
        "notAvailableDate": "2019-02-25 10:00:00"
    }
],
"location":{
    "identifier":"UBZ190asas11",
    "name": "abulalas,purok 4",
    "address" : "abulalas1 hagonoy bulacan",
    "lat" : 12141.00,
    "lng" : 123251.00
},
"units": 2,
"title": "sample unit content",
"catalogId": 6,
"categoryId": 22,
"subcategoryId": 13,
"contentOptions": [ 
     {
        "name":"bannana boat",
        "maxPax":8,
        "isAvailableDayTime":[
            9,10,11,12,13,15,16,17,18,
            33,34,35,36,37,39,38,39,40,
            56,57,58,59,60,62,63,64,65,
            80,81,82,83,84,86,87,88,89,
            104,105,106,107,108,110,111,112,113,
            128,129,130,131,132,134,135,136,137,
            152,153,154,155,156,158,159,160,161
        ],
        "inventoryNeededSet":[
            {
            "inventoryId": 1,
            "count":1
            },
            {
            "inventoryId": 1,
            "count":2
            }
        ],
        "paxPrices": [
            {
                "count": 5,
                "pricePerPax": 200,
                "totalPrice": 1000,
                "fee": 100
            },
            {
                "count": 1,
                "pricePerPax": 200,
                "totalPrice": 200,
                "fee": 10
            }
        ]
     },
     {
        "name":"bannana with island tour",
        "maxPax":10,
        "isAvailableDayTime":[
            9,10,11,12,13,15,16,17,18,
            33,34,35,36,37,39,38,39,40,
            56,57,58,59,60,62,63,64,65,
            80,81,82,83,84,86,87,88,89,
            104,105,106,107,108,110,111,112,113,
            128,129,130,131,132,134,135,136,137,
            152,153,154,155,156,158,159,160,161
        ],
        "inventoryNeededSet":[
            {
            "inventoryId": 1,
            "count":2
            },
            {
            "inventoryId": 1,
            "count":2
            }
        ],
        "paxPrices": [
            {
                "count": 5,
                "pricePerPax": 200,
                "totalPrice": 1000,
                "fee": 100
            },
            {
                "count": 1,
                "pricePerPax": 200,
                "totalPrice": 200,
                "fee": 10
            }
        ]
     }

],
"photos": [
    "https://samplephoto1.com",
    "https://samplephoto2.com",
    "https://samplephoto3.com"
],
"videos": [
    "https://samplevid1.com",
    "https://samplevid2.com",
    "https://samplevid3.com"
],
"why": "sample why",
"description": "sample desc",
"cancellationPolicy":"cancellationPolicy",
"displayPrice": 300,
"displayDiscounted": 250,
"displayMaxPax": 2

}

问题是,我想保存冻结日期,插入冻结日期的语法是什么?

=======================已更新======================

在对数据变量进行字符串化之前,请尝试以下操作:

data.blockdates = $("#block-dates").val();

要执行您的代码,需要jQuery 在代码之前插入<script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>后尝试。

如果let blockdates = $("#block-dates").val();

您可以像这样将blockdates追加到data

data['blockdates']=blockdates;

您可能需要先将元素保留在对象中。 然后可以将它们添加到阵列中。

blockDates= [];
var description = $("#card-description").val();
var notAvailableDate = $("##block-dates").val();

var blockdate = {description, notAvailableDate};

blockDates.push(blockdate);

in this way => let content = ajax("api/unitContents", JSON.stringify(data, blockDates), "POST");

要么

    let data = {
          "title": title,
          "catalogId": catalogId,
          "categoryId": categoryId,
          "subcategoryId": subcategoryId,
          "why": why,
          "cancellationPolicy": cancellationPolicy,
          "displayPrice": displayPrice,
          "displayDiscounted": displayDiscounted,
          "displayMaxPax": displayMaxPax,
          "blockDates": blockDates 
        };

in this way => `let content = ajax("api/unitContents", JSON.stringify(data), "POST");`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM