Transfer an array into data using the POST method

When making a REST API call in CodeIgniter, I encountered an issue with the array format being sent to the server:

[{"PMcolor":"Azul tostado","PMpartes":"Un poquito de las orjeas y un bigote a lo Dali, quizas le alegre la cara","PMcosteTotal":"445"}]:

The object I am working with is:

myobject = {PMcolor: "Azul tostado", PMpartes: "Un poquito de las orjeas y un bigote a lo Dali, quizas le alegre la cara", PMcosteTotal: "445" };

I have tried the following methods for POST requests:

1)

$scope.datosEnviar = [];
    $scope.datosEnviar.push(myobject);

  var config={ //this works, DO NOT CHANGE, this is for post method
    method:"POST",
    url:"http://localhost/APIREST/controllersencillo/", 
    params: {tabla : "PintaMonas"} 
    ,data:  $scope.datosEnviar,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  }

2)

var config={ //this works, DO NOT CHANGE, this is for post method
    method:"POST",
    url:"http://localhost/APIREST/controllersencillo/", 
    params: {tabla : "PintaMonas"} //with id update, without id insert
    ,data:  myobject,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  }

Answer №1

I have come across a partial solution, however I am facing a challenge where the array needs to be sent in string format. I now need to figure out how to send the array in an actual array format.

$scope.dataToSend = [myobject];
    /*$scope.dataToSend.push(myobject.PMcolor);
    $scope.dataToSend.push(myobject.PMpartes);
    $scope.dataToSend.push(myobject.PMcosteTotal);*/

  var config={ //this works fine, DO NOT ALTER, this is for post
    method:"POST",
    url:"http://localhost/APIREST/controllersencillo/", //id: JSON.stringify(ids) //{PMcolor: "Azul tostado", PMpartes: "Un poquito de las orjeas y un bigote a lo Dali", PMcosteTotal: "445" }
    params: {tabla : "PintaMonas"/*, data: JSON.stringify($scope.datosEnviar)*/} //no id means insert, with id means update
    ,data: 'PMcolor='+ JSON.stringify($scope.dataToSend),//myobject.PMcolor+", PMpartes="+myobject.PMpartes+", PMcosteTotal="+myobject.PMcosteTotal,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  }

Now I need to send the following array [PMcolor] => [{"PMcolor":"Azul tostado","PMpartes":"Un poquito de las orjeas and un bigote a lo Dali, quizas le alegre la cara","PMcosteTotal":"445"}], but the data is currently in string format.

Answer №2

Issue Resolved:

,data: 'PMcolor='+myobject.PMcolor+"&PMparts="+myobject.PMparts+"&PMtotalCost="+myobject.PMtotalCost,

Although not an array, it does the job

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the proper way to implement the scrollToIndex feature in a FlatList component in React Native

I have a problem where I need to automatically scroll down by a specific value whenever a onPress event is triggered (in this case, the value is 499). However, the code I have tried does not seem to be working as expected. Here is the code snippet I am u ...

Tips for resolving the issue of "Warning: validateDOMNesting(...): <div> cannot be a child of <tbody>"

Users list is passed as a prop to the UserItem Component in order to iterate over the user list and display them on a table. The list is being displayed correctly, and there are no divs in the render return, but an error persists: tried many solutions fou ...

Vue: the parent template does not permit the use of v-for directives

Upon creating a simple post list component, I encountered an error when trying to utilize the v-for directive: "eslint-eslint: the template root disallows v-for directives" How can I go about iterating through and displaying each post? To pass data from ...

JavaScript game with server-side communication and answer validation functionality

In my fast-paced, quiz-like Javascript game, users must answer a series of Yes/No questions as quickly as possible. Upon answering, the response is sent to the server for validation and feedback (correct/incorrect) before moving on to the next question usi ...

Exploring Keypress events with Dojo's 'on' module

Recently, I've started utilizing Dojo's latest on module for event handling. It has been working well so far, but a new issue has cropped up. Specifically, when using the keypress event, I am unable to retrieve the character value (such as "2" or ...

Adjust the margin of a child div based on the parent's width, with the use of JavaScript

Currently, I am developing a website at and I am interested in replicating the layout of another site. The site I would like to emulate is , which features multiple sections with child divs that have margins around them. In order to achieve this effect o ...

Attempting to execute npm install for an Odin project task, encountered the error "Module not Found". // A new error has surfaced, continue reading below

Trying to run npm install for the Odin Project JavaScript Fundamentals Part 4 lesson has been quite a challenge. Initially, upon forking and cloning the repository and running npm install as per the instructions, I encountered a permission error. However, ...

Tips for Utilizing Environmental Variables within a Vue Application's index.html File

My website has an index file with all the necessary meta tags, stylesheets, and scripts: <!DOCTYPE html> <html lang="en"> <head> <!-- Required Meta --> <meta charset="UTF-8"> <meta http-equi ...

Show information in a table based on a unique identifier

I am working with some data that looks like this [ { date: '20 Apr', maths: [70, 80.5, 100], science: [25, 20.1, 30] }, { date: '21 Apr', maths: [64, 76, 80], science: [21, 25, 27] }, ]; My goal is to present ...

Issue with default behavior of infinite scroll in Angular 4

I'm currently working on incorporating infinite scroll into my Angular 4 application. I've carefully followed all the guidelines provided on https://www.npmjs.com/package/ngx-infinite-scroll According to the documentation: By default, the dir ...

I am looking to dynamically add values into a hash map

var markerList1={}; var markerList=[]; and incorporating iterator values from a single for loop function addSomething() // this function will run multiple times from a for loop { image ='../css/abc/'+image[iterator]+'.png&apos ...

emulate clicking on a child component element within the parent component's test file

In my testing scenario, I encountered a challenge in simulating the click event of an element that exists in a child component from the parent test file. let card; const displayCardSection = (cardName) => { card = cardName; }; describe('Parent ...

What is the best way to implement Redux within Next.js 13?

Currently, I am using Next JS 13 with Redux. In Next.js 12, I was able to wrap my entire application with Provider inside ./pages/_app. However, how can I achieve this in Next JS 13? Here is the code from my layout.js: import "../styles/globals.css&q ...

How to Send an Array to AJAX and Retrieve the Data in Codeigniter Controller

I am attempting to retrieve table data, store it in an array, and pass it to the controller so I can write it in PHP Excel. Previously, I had success with other data but now my excel file is turning up empty. Below is the JavaScript code snippet: var Ta ...

Learn how to effectively manage an element within an HTML-5 document using Protractor

I am new to Protractor and my task involves automating third-party tools. I encountered an issue where I couldn't locate a specific web element that changes its state and pulls data from another application when clicked, causing its class value to cha ...

What is the best way to retrieve the returned value from a jQuery GET call?

I was looking to implement something along these lines: function example(); var result = example(); if(result == 1) However, in my example function, I am making a Get request and using a callback that is not returning the value correctly as the ...

Experiencing a Number TypeError Issue with Mongoose Schema?

The server encountered a 500 internal error with the message: Error: TypeError: path must be a string The specific line causing the error in ItemCtrl.js is line 35. console.log('Error: ' + data); The stack trace for this error is as follows: ...

I am attempting to send an array as parameters in an httpservice request, but the parameters are being evaluated as an empty array

Trying to upload multiple images involves converting the image into a base64 encoded string and storing its metadata with an array. The reference to the image path is stored in the database, so the functionality is written in the backend for insertion. Ho ...

How to choose the option in one select box based on the selection in another, and vice versa

How can I dynamically select options in one select box based on the selection of another, and vice versa? I am using Ajax to redirect to a query page. while($result = mysql_fetch_assoc($query1)) { echo "<option value=".$result['username' ...

Pacman-inspired Javascript game - scoring limitations on horizontal paths

I'm currently working on a fun project involving JavaScript that requires creating a ninja-like game similar to pacman. The objective is to control the ninja to eat sushis and earn points based on each sushi eaten. At the moment, I am facing an issue ...