To send a key-value pair array in a POST request in JavaScript, you can use the XMLHttpRequest
object to create the request and JSON.stringify
to serialize the data in JSON format. Here's an example:
index.tsx506 chars17 lines
In this example, we define an object data
with some key-value pairs. We also specify the URL of the API endpoint in url
. Then, we create a new XMLHttpRequest
object and open a POST request to the specified URL. We set the Content-Type
header to indicate that we're sending JSON data. We define the onload
and onerror
event handlers to handle the response or error. Finally, we send the POST request with the serialized JSON data using the send
method.
gistlibby LogSnag