I am writing a script to submit form data to the web server of a Siemens S7-1200 PLC without reloading the page. So far I have put the following together using various examples and my (limited) JavaScript knowledge.
Code:
var postrequest; // creates new variable for the XMLHttp object
if (window.XMLHttpRequest)
{ // XMLHttp code for IE7+, Firefox, Chrome, Opera, Safari
postrequest=new XMLHttpRequest();
}
else
{ // XMLHttp code for IE6, IE5
postrequest=new ActiveXObject("Microsoft.XMLHTTP");
}
function ajaxPost(elem) // function sends the data to the server. "elem" is the DOM of the button that called it.
{
var name=encodeURIComponent(elem.previousSibling.name); // Fetches the "name" atribute from the element before the button that called this function, encodes any special charactors.
var value=encodeURIComponent(elem.previousSibling.value); // Fetches the "value" atribute from the element before the button that called this function, encodes any special charactors.
var parameters=name.concat("=",value); // Creates the string to send based on the fetched name and value in the format "name=value", for aditional form elements change this to "name1=value1&name2=value2"
postrequest.open("POST", "post.html", true); //makes the post request, as the webserver returns the entire file the post was made to I have created a seperate html file which only includes the needed information to post to unlike the example in the manual which uses a form to post to itself. This should save on bandwidth.
postrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //sets the header
postrequest.send(parameters); //sends the string
// document.write(parameters); //debug
}
/*
Expected Call:
<form>
<input type="text" value="1" name='"Web_Access".DQ0a'/><input class="onoff" type="button" value="On" onClick="ajaxPost(this)"/>
</form>
*/
Due to the way the web server works post.html is a document that lists all the tags that a post request should be intercepted and used to modify the PLC tags, I have no control over that, the web server does not support php or asp, only it's own propriety method of receiving data.
My script works perfectly in Firefox and Internet Explorer but does not work in Chrome, the dev tools say the request is "pending" for ages. Can anyone see where I have gone wrong?
Thanks,
Jon