Solved

Convert HTTP post to JSON Post

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

830 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