data example we want to send by ajax
const dataExample = {
"userId": '...id',
"supportFormId": 14,
"supportFormName": 'Tickets',
"customFields": [
{
"customFieldId": 80,
"name": 'Subject',
"value": '...',
"dataType": 'Text'
},
// ....
{
"customFieldId": 84,
"name": 'Description',
"value": '...',
"dataType": 'Text'
}
]
}
jQuery ajax call
$.ajax({
type: 'post',
url: 'http://....',
dataType: 'json',
data: dataExample,
success: function (data) { /* ... */ }
});
axios + query-string ajax call
import axios from "axios";
import qs from 'query-string'
const dataQs = qs.stringify(dataExample);
return new Promise(
async (resolve, reject) => {
try {
const response = await axios({
method: 'post',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: dataQs,
url: 'http://....'
});
if (response) return resolve(response)
return reject()
} catch (err) { return reject(err) }
}
);
result
question
jQuery never has any type of problem but axios + query-string, despite different headers like
'content-type': 'application/json',
'content-type': 'multipart/form-data',
and/or optional stringify options like
const dataQs = qs.stringify(data, { encode: false })
const dataQs = qs.stringify(data, { arrayFormat: 'indices', commaSuffix: 'bracket' })
const dataQs = qs.stringify(data, { arrayFormat: 'indices' })
const dataQs = qs.stringify(data, { arrayFormat: 'brackets' })
const dataQs = qs.stringify(data, { arrayFormat: 'repeat' })
const dataQs = qs.stringify(data, { arrayFormat: 'comma' })
breaks the data all the times..
which is the correct axios + query-string (or alternative) code to get the same result of jQuery?

JSON.stringifyand useContent-Type: application/jsonto try. Just a guess.