0

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.

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)));
};`
Michael Johns
  • 419
  • 3
  • 21
  • Long answer: See [this answer](https://stackoverflow.com/a/6085669/2487517) and [this documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send). Short answer: `req.send( { action: ..., submittingURL: ... })` – Tibrogargan May 16 '18 at 23:19
  • I can't use jQuery. – Michael Johns May 17 '18 at 17:32
  • jQuery uses exactly the same mechanism under the covers. Read the documentation. You're going to end up using one of the alternatives arguments of `XMLHttpRequest.send()` to send your data. Short answer remains the same – Tibrogargan May 17 '18 at 18:00

0 Answers0