简体   繁体   中英

$.param serialized object to JSON

I send data from client to server ( Python ) using $.fileDownload . API receives the data but, as the jQuery fileDownload docs state, the data is serialiazed using $.param . The problem is that I need to have the data in JSON format so that I can handle it and create a file using it's key:value pairs . I've been trying to figure this out but nothing seems to be done in order to "deserialize" it. It also seems that I can't make $.fileDownload to send data as a JSON object in the first place. Is there any way that I turn the serialized data to JSON?

Here is my JavaScript request:

let data = {
  'name': item.Name,
  'rows': item.rows
}

window.$.fileDownload('/api/export-report-excel', {
  httpMethod: 'POST',
  encodeHTMLEntities: true,
  data: data
})

In my API I've tried json methods (json.dumps etc.) but the serialized object cannot be converted.

The $.fileDownload states:

// data must be an object (which will be $.param serialized) or already a key=value param string

EDIT:

In my Falcon API :

@route('/api/export-report-excel')
class ExportReportToExcel(object):

  def on_post(self, req, resp):
    data = req.stream.read()
    log.info(data)

log.info(data) output:

b'name=Priority+Rules&rows%5B0%5D%5BSegment%5D=HH+wo+TV&rows%5B0%5D%5BValue%5D=6&rows%5B0%5D%5BCampaignType%5D=CampaignType%3F&rows%5B0%5D%5BCampaignSubtype%5D=Predefined+Contract&rows%5B0%5D%5BChannel%5D=WIND+STORE&rows%5B0%5D%5BPriority%5D=1&rows%5B1%5D%5BSegment%5D=HH+wo+TV&rows%5B1%5D%5BValue%5D=6&rows%5B1%5D%5BCampaignType%5D=CampaignType%3F&rows%5B1%5D%5BCampaignSubtype%5D=Predefined+Contract&rows%5B1%5D%5BChannel%5D=SMART+WIND+STORE+MALL&rows%5B1%5D%5BPriority%5D=2'

This is form-encoded data, which you can parse in Python using urllib.parse.parse_qs .

>>> from urllib import parse
>>> parse.parse_qs('name=Priority+Rules&rows%5B0%5D%5BSegment%5D=HH+wo+TV&rows%5B0%5D%5BValue%5D=6&rows%5B0%5D%5BCampaignType%5D=CampaignType%3F&rows%5B0%5D%5BCampaignSubtype%5D=Predefined+Contract&rows%5B0%5D%5BChannel%5D=WIND+STORE&rows%5B0%5D%5BPriority%5D=1&rows%5B1%5D%5BSegment%5D=HH+wo+TV&rows%5B1%5D%5BValue%5D=6&rows%5B1%5D%5BCampaignType%5D=CampaignType%3F&rows%5B1%5D%5BCampaignSubtype%5D=Predefined+Contract&rows%5B1%5D%5BChannel%5D=SMART+WIND+STORE+MALL&rows%5B1%5D%5BPriority%5D=2')

{'name': ['Priority Rules'],
 'rows[0][Segment]': ['HH wo TV'],
 'rows[0][Value]': ['6'],
 'rows[0][CampaignType]': ['CampaignType?'],
 'rows[0][CampaignSubtype]': ['Predefined Contract'],
 'rows[0][Channel]': ['WIND STORE'],
 'rows[0][Priority]': ['1'],
 'rows[1][Segment]': ['HH wo TV'],
 'rows[1][Value]': ['6'],
 'rows[1][CampaignType]': ['CampaignType?'],
 'rows[1][CampaignSubtype]': ['Predefined Contract'],
 'rows[1][Channel]': ['SMART WIND STORE MALL'],
 'rows[1][Priority]': ['2']}

I don't know Falcon, but I am sure that it has a way of accessing this data directly, without accessing the raw stream or passing into parse_qs . In Django or Flask you would get it via request.POST , which is a dictionary; skimming through the Falcon docs, it looks like req.params might do a similar job, although you may need to set the auto_parse_form_urlencoded option to True first.

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