I need to simulate submitting a form on an external site, I already had it working for other sites but now the external site expects json formatted values as parameters which is being hard to accomplish.
Before the server expects json I had it perfectly working with this (note the javascript simulates submitting the form, and the form is coded to relate to the external site):
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function load()
{
window.document.login_form.submit();
return;
}
</script>
</head>
<body onload="load()">
<form name="login_form" method="post" action="http://external.webserver.com/web/session/authenticate">
<input type="hidden" name="name" value="andres" />
<input type="hidden" name="phone" value="593123123" />
</form>
Here I attacha an image that profs that it will work if I would post the body as json data.
As a solution I've tried other ideas like suggested here POST data in JSON format
function load()
{
var frm = $(document.login_form);
dat = JSON.stringify(frm.serializeArray());
alert("I am about to POST this:\n\n" + dat);
$.post(
frm.attr("action"),
dat,
function(data) {
alert("Response: " + data);
}
);
//window.document.login_form.submit();
return;
}
But that approach takes me several other problems regarding related to 'Access-Control-Allow-Origin' which I won't fix as I don't own the server to which I want to post.
So would any one provide a better approach to make a json post (or post the body) to an external server?