Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    AJAX Form submit won't work in Chrome

    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

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 565 Times in 558 Posts
    This is not valid HTML
    Code:
    <input type="text" value="1" name='"Web_Access".DQ0a'/>
    Where is this .DQ0a' coming from?

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    "Web_Access".DQ0a is the name of the PLC tag I need to change, using a form input in that method is how the PLC expects it. The manual has it written as I have, but with the button a submit type instead (and no onclick).

    Why is that invalid by the way? Can't you have whatever you like in the name string as long as it is between "_" or '_' ?

    I think the problem must be in the post part because when I take out the comments on the document.write(parameters); line I get the same result in Firefox and Chrome: %22Web_Access%22.DQ0a=1
    Last edited by jon_hill987; 04-10-2012 at 04:41 PM.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 565 Times in 558 Posts
    Ok ... it might be valid although quite unusual. At first glimpse I saw a quote mismatch, but there is none.

  • #5
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah yeah, I know what you mean, having to use that format does not make it very readable, in some editors you can hardly see the gap between ' and "...

    I didn't mean to come across as trying to correct you by the way, I was genuinely interested in what you had seen that made it invalid if that was the problem as I am fairly new to all this.

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well it is worse than I thought, not only does it not work in Chrome but, if you press the button repeatedly, you can actually crash the PLC (web server). Does anyone know what is different about the way Chrome handles XMLHttpRequest posts? I can't see any difference in the data that is being sent in Firefox and Chrome and can't understand why one would crash it and the other not. Have I used the request wrong and IE and Firefox correct my error where Chrome will not? My other thought (as it can crash it) is that there might be a firmware bug in the PLC, but that does not explain how it managed to work fine in two out of the three browsers I have tested and as they don't technically support anything other than HTML (there is provision for client side scripting but the manual pretty much says that you are on your own) I need to rule out my script being wrong before I let them know I think there is a firmware issue.

    I'm getting to the point where the only solution I can think of is to have it post using an html form if it detects a browser other than the ones I know to work, is that feasible? I understand browser detection can be a pain...

    EDIT: So I have got around my issue with browser detection, submitting the data with an html form if it is not IE or Firefox. Interestingly Opera does exactly the same as Chrome, the submit will not work and repeated presses can crash the server. I take it no one has come across this before?
    Last edited by jon_hill987; 04-13-2012 at 01:23 PM. Reason: New info


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •