简体   繁体   中英

How can I pass a map result to $.ajax jQuery function

I am retrieving values from html component as value-pair in jQuery.

var items = $('.bid').map(function () {
    return { "date": $(this).attr('date'), "Id": $(this).attr('qId') };
});

The output items gives me the following:

0: {date: "01/09/2018 10:55:39", Id: "1626"}
1: {date: "03/09/2018 10:55:39", Id: "1674"}
2: {date: "05/09/2018 22:55:39", Id: "1624"}

How can I pass the following to $.ajax() in jQuery ?

$.ajax({
  type: 'POST',
  url: '@Url.Action("Create", "Cars")',
  data: {
    dataObj: items
  },
  success: function (resp) {
    console.log('success');
  },
  error: function () {
    console.log('error');
  }
});

You can use,

JSON.stringify()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

in the javascript and decode it in the php with

json_decode();

http://php.net/manual/en/function.json-decode.php

also if you want to pass an array to client site again you can use,

json_encode()

http://php.net/manual/en/function.json-encode.php

and echo it. and decode it in client site with

JSON.parse()

https://www.w3schools.com/js/js_json_parse.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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