Tengo una variedad de objetos como este.
var arr = [ {name: 'myName', age: 25, city: 'myCity'}, {name: 'myName1', age: 25, city: 'myCity1'} ]; Ahora, necesito pasar esta matriz arr a una carga útil como un objeto JSON. JSON.stringify(arr) y luego pasé los datos. Pero se está convirtiendo en un formulario de datos y el formato de carga útil es como el siguiente;
[{name: 'myName', age: 25, city: 'myCity'},{name: 'myName1', age: 25, city: 'myCity1'}]:Si ha notado dos puntos al final de la carga útil, parece que convirtió mi matriz como un nombre de propiedad en los datos del formulario. ¿Puede ayudarme a pasar la matriz como un objeto JSON?
El código que se utiliza para llamar al método API es,
{ type: 'POST', method: 'POST', dataType: 'json', url: 'url of the api call', beforeSend: function(header, settingsData){ Object.keys(settingsData.options).forEach(headerKey){ headerKey.setHeader(headerKey, settingsData.options[headerKey]); } return settingsData; }, disableDefaultError: true }Mi comentario era medio correcto.
dataType: 'json'contentType: 'application/json' ya que el valor predeterminado es 'application/x-www-form-urlencoded; charset=UTF-8' { type: 'POST', method: 'POST', dataType: 'json', contentType: 'application/json', // <- this is needed url: 'url of the api call', beforeSend: function(header, settingsData){ Object.keys(settingsData.options).forEach(headerKey){ headerKey.setHeader(headerKey, settingsData.options[headerKey]); } return settingsData; }, disableDefaultError: true }