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

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!
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
894359Author Commented:
Let me look deeper into this and I'll let you know. Appreciate the input.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.