How to post parameters to a url using Ajax/Javascript between two website

· ☕ 3 min read · ✍️ Dinesh

I am working a new project and I recently ran into an interesting problem. One of the web site that I keep up at work was supposed to take the user to another website which required me to add post parameters.

EX - ==> Collects Billing information ex - name, amount, address etc.==> Post this information to

I did not realize the problem until I started coding and my colleague pointed out that as soon as goes to my servlet, the servlet will not pass params to external web site if use “POST”, I had to use “GET” because servlet will look up relative path only and the HTTPServletRequest/Response object is specific to an application. So if I wanted to send parameters using servlet I could only that using action = GET. Now since I was passing sensitive information so I did not want to use GET.

A couple of solutions were discussed as follows:

  1. Insert the params in database and use servlet get from the to pass the primary key of the database. Ex - ==> The vendor application look up the required params from the database.

  2. Create a new JSP and use JavaScript onLoad() to pass the params as Hidden Input and submit as post to

Ex -> Servlet==> New Blank JSP with Hidden params loaded on onLoad() and submitted to vendor website ==>

  1. Third approach is interesting and I had not tried this ever but looked promising and this is what I eventually implemented. Make an Ajax call to from the JSP page to your servlet and when the Ajax Call returns, post it to vendor web site.

Ex - on hitting submit==> calls the JS, uses DWR to post to call the servlet==> Servlet does back ground processing like saving the records etc ==> Returns the control back to the JavaScript ==> Upon return in Ajax Call ==> Post to

I implemented the combination of one and three but here I am going to show you how post params to a different URL - i.e. solution 3

Let’s say I have a submit form with Name and address which needs to be saved in database when I submit the form and then I need to post the same information to different web site.





>      // I am not showing the code for DWR. You will need to include dwr and engine.js. Add dwr.xml in your web-inf and specify the class name and method you want to use as dwr call. This method is called in the dwr Ajax call back.

 function submitAndGoToVendorSite()
  var form = document.createElement("FORM");
  form.method = "POST"; = "none";
  var url="";
  form.action = url;

            //My dwr ajax call gets a Json string from the servlet response.
 var jsonString =      "{"transactionId":"1368505156670","requesterType":"APP","billingEmail":"","billingState":"NC","amount":"42.7699999999999999433786257441170164384","refund":"N","billingCity":"CONCORD","billingLine1":"Progress Pl","billingFirstname":"test","billingLine2":"","shopperIP":"","application":"OEP2","currencyCode":"USD","billingCompany":"ACN","revenueSource":"","billingLastname":"test","countryCode":"US","billingAddrNum":"1000","cardType":"VISA","businessPurpose":"TOOL","profileConfig":"cybersource-MLTEST1","language":"en","billingZip":"28025","repOrCustID":"1233836","user":"DARORATEST","paymentMethod":"CC","billingPhone":""}'

     // Create a JSON object from the JSON String
     var jsonObj = jQuery.parseJSON(jsonString);

//Iterate over Json object and set them as hidden input params to the form
 for(obj in jsonObj)
   var input = document.createElement("INPUT");
   input.type = "hidden"; = obj;
   input.value = jsonObj[obj];

 //Submit the form





            <label>Name:</label><Input type="text"/>


            <input type="button" onclick="submitAndGoToVendorSite();"/>



That’s It!

~Keep Coding

Share this post
Support the author with

Dinesh Arora