JavaScript - How to Send Mixed Content Type in an Ajax Post?

894359
894359 used Ask the Experts™
on
This is similar to a previous post I made but I'm still confused and need help. I have two pieces of data to send to a service. I need to send, using javascript, an "action" which is JSON and I need to send a "submittingURL" which is just the encoded URL. Is there a way to send both pieces of data in the POST? If so how? This is all new to me. Below is the same function sending both as a GET and then the second one splits it so the JSON is POSTed. How can I send both via POST even though mixed content types (no jQuery)?
function sendUnapprovedAction(action) {
    var req = new XMLHttpRequest();
    var url = '//001l60fbadm:10001/marketingcampaigns/api/v1/campaigns?action=' +
    encodeURIComponent(JSON.stringify(action)) + '&submittingURL=' + 
    encodeURIComponent(_satellite.getVar("Full URL"));
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/json');
    req.send();
};

function sendUnapprovedAction(action) {
    var req = new XMLHttpRequest();
    var url = '//001l60fbadm:10001/marketingcampaigns/api/v1/campaigns?submittingURL=' +
    encodeURIComponent(_satellite.getVar("Full URL"));
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/json');
    req.send('action=' + encodeURIComponent(JSON.stringify(action)));
};`

Open in new window


Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This is not easy to answer because you are not giving us enough information. How you do this is dependent on the service you are connecting to. There are no such things as types in a XHR request - only strings.
POST data can be anything you want - it does not have to be a particular format - it can be your own.
Typically applications recognise the following
URL Encoded
param=1&anotherparam='Some value'

Open in new window

JSON
{"param1":1, "anotherparam":"Some Value"}

Open in new window

Multipart (usually for files)
As you can see it is just string data - the only important thing you need to do is make sure that string data is in a form the receiving service is expecting.

If you are wanting to send your URL and JSON data as URL Encoded parameters i.e. param=1&anotherparam='Some Value' then you can do it as shown below. Otherwise you will need to provide us with some spec that defines how the receiving service is expecting to receive this data.
function sendUnapprovedAction(action) {
  var encodedUrl = encodeURIComponent(_satellite.getVar("Full URL"));
  var json = JSON.stringify(action);
  var postData = 'submittingURL=' +  encodedUrl + '&action=' &  json;

    var req = new XMLHttpRequest();
    var url = '//001l60fbadm:10001/marketingcampaigns/api/v1/campaigns';
    req.open('POST', url, true);
    req.setRequestHeader('Content-Type', 'application/json');
    req.send(postData);
};`

Open in new window

Author

Commented:
Let me look deeper into this and I'll let you know. Appreciate the input.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial