POST array data with javascript POST array data with javascript arrays arrays

POST array data with javascript


I'm assuming you're making a POST request with a JSON payload. First, you want to make sure your payload is formatted JSON correctly, use: http://pro.jsonlint.com/

Secondly, you can send the payload using JSON.stringify and you will want to set the contentType:

data: JSON.stringify(data),contentType: "application/json; charset=utf-8"

If you run this and look at your network tab in Dev Tools in Chrome, it will error out, but you will at least see the payload formatted to send to the server:http://prntscr.com/f8hf6s

var data = [{    "key": "myKey",    "keyName": "myKeyName",    "value": "value",    "valueName": "valueName"  },  {    "key": "mySecondKey",    "keyName": "mySecondKeyName",    "value": "secondValue",    "valueName": "secondValueName"  }];var url = "http://swapi.co/api/";$.ajax({  type: "POST",  url: url,  data: JSON.stringify(data),  contentType: "application/json; charset=utf-8",  dataType: "json",  error: function() {    alert("Error");  },  success: function() {    alert("OK");  }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>