Solved

Convert HTTP post to JSON Post

Posted on 2013-12-20
1
487 Views
Last Modified: 2013-12-27
I have a .NET application that was doing a regular HTTP post to a third party server. The third party will not longer accept a regular HTTP post and now they want a JSON post. because the way the form is set up, I cannot change the form to do the JSON post so I would like to create some kind of bridge page to receive HTTP and then do the JSON post.

Is this possible?, if so what is the best way to go about it?
0
Comment
Question by:Benjosp
1 Comment
 
LVL 16

Accepted Solution

by:
kiranvj earned 500 total points
ID: 39732804
Do a AJax post. Serialize your form data and send it to the third party server as a POST request.

You can use jQuery or plain JavaScript for this.

jQuery

Check this example on how to serialize data http://jsfiddle.net/sxGtM/3/

For plain JS

Use this javascript to convert your data in form to Json

// collect the form data while iterating over the inputs
  var data = {};
  for (var i = 0, ii = form.length; i < ii; ++i) {
    var input = form[i];
    if (input.name) {
      data[input.name] = input.value;
    }
  }

// construct an HTTP request
  var xhr = new XMLHttpRequest();
  xhr.open(form.method, form.action, true);
  xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

  // send the collected data as JSON
  xhr.send(JSON.stringify(data));

  xhr.onloadend = function () {
    // done
  };
};

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now