Solved

Convert HTTP post to JSON Post

Posted on 2013-12-20
1
481 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now