Results 1 to 10 of 10

Thread: Javascript newbie - POST request

  1. #1
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Javascript newbie - POST request

    I have a javascript function provided by a sign manufacturer to set of GPI triggers. I tried to write a simple web form to submit POST requests to fire the javascript. I'm getting the "Error: Object Expected" from the following:

    HTML Code:
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script type="text/javascript">
    function fire(gpi_index)
    {
    	method="POST";
    	url="http://localhost:8009/maxidrivers/maxisoftgpi/fire?gpi=" + gpi_index;
    	async = false;
    
    	if (window.ActiveXObject) {
    		// MS IE
    		try
    		{
    			var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			httpRequest.Open(method, url, async);
    			httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			httpRequest.Send(send);
    		}
    		catch (e)
    		{
    			return ("Error: " + url + "\n" + e);
    
    		}
    	} else if (window.XMLHttpRequest) {
    		// Not MS IE
    		try
    		{
    			var httpRequest = new XMLHttpRequest();
    			httpRequest.open(method, url, async);
    			httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			httpRequest.send(send);
    		}
    		catch (e)
    		{
    			return("Error: " + url + "\n" + e);
    			
    		}
    	}
    
    
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Channel Changer</title>
    </head>
    
    <body>
    <form action="index.php">
    <input type="button" value="1" onclick="fire()" /><br />
    <input type="button" value="2" onclick="fire()" /><br />
    <input type="button" value="5" onclick="fire()" /><br />
    </form>
    </body>
    </html>

  2. #2
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    Oh yeah , the error message the "Error: Object Expected" refers to these lines:

    HTML Code:
    <input type="button" value="1" onclick="fire()" /><br />
    <input type="button" value="2" onclick="fire()" /><br />
    <input type="button" value="5" onclick="fire()" /><br />
    I've also tried "onclick="fire(1)"> with the same results.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your function has no closing brace, add it here (highlighted red):

    Code:
     . . . pRequest = new XMLHttpRequest();
    			httpRequest.open(method, url, async);
    			httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			httpRequest.send(send);
    		}
    		catch (e)
    		{
    			return("Error: " + url + "\n" + e);
    			
    		}
    	}
    }
    
    </script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Channel Changer</title>
    </head> . . .
    There could also be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    rd42 (04-24-2009)

  5. #4
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    I have tried the code, and there was a problem with it.

    There is a missing "}" before the </script>

    Other than this I didn't get any "Error: Object Expected" message.


    Are you sure it is in the piece of code you gave.

  6. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    rd42 (04-24-2009)

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by forum_amnesiac View Post
    I have tried the code, and there was a problem with it.

    There is a missing "}" before the </script>

    Other than this I didn't get any "Error: Object Expected" message.


    Are you sure it is in the piece of code you gave.
    Without that brace, IE gives the error described. But as I note above, there could also be other problems.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. The Following User Says Thank You to jscheuer1 For This Useful Post:

    rd42 (04-24-2009)

  9. #6
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    Thank you all very much for your help. I added the } with no luck.
    Maybe I'm going about this the wrong way. All I need to do is load these links without actually going to the page. They trigger a channel change in a digital sign.



    Unfortunately the company has some inaccessible web server in the PC doing this and the files are compiled into something I can't change. I been ask to reformat the generated page with "pretty" buttons.

    All I got from tech support so far is in the attachment.

    Again, thanks very much for the help, any suggestions, and finding that missing bracket

    Robert

  10. #7
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    I've opened the .txt file and there is nothing of value in it, are there more files you can post

  11. The Following User Says Thank You to forum_amnesiac For This Useful Post:

    rd42 (04-24-2009)

  12. #8
    Join Date
    Oct 2008
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default

    Wierd, the file has content on my pc, here it is the attachment:

    __________Attachment_____________________________


    Url to fire GPI #5 looks like this:
    http://localhost:8009/maxidrivers/ma...gpi/fire?gpi=5

    Replace 'localhost' with the name of the host, if you're doing it remotely.
    and of course, replace '5' with the # of the GPI to fire. Post anything you want to that url, and it goes.

    You issue an HTTP POST request the Phoenix HTTP server port, which is usually: localhost:8009 The resource path is: /maxidrivers/maxisoftgpi/fire you issue it with a query of gpi=x where x is the number of the GPI you want to fire

    You can also invoke the function: fire(x) ...in the javascript hosted at http://localhost:8009/maxidrivers/maxisoftgpi/main.js

    Or, if you need to completely do it yourself:

    Sample javascript to invoke an http POST request:

    Code:
    function fire(gpi_index)
    {
    	method="POST";
    	url="http://localhost:8009/maxidrivers/maxisoftgpi/fire?gpi=" + gpi_index;
    	async = false;
    
    	if (window.ActiveXObject) {
    		// MS IE
    		try
    		{
    			var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			httpRequest.Open(method, url, async);
    			httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			httpRequest.Send(send);
    		}
    		catch (e)
    		{
    			return ("Error: " + url + "\n" + e);
    
    		}
    	} else if (window.XMLHttpRequest) {
    		// Not MS IE
    		try
    		{
    			var httpRequest = new XMLHttpRequest();
    			httpRequest.open(method, url, async);
    			httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    			httpRequest.send(send);
    		}
    		catch (e)
    		{
    			return("Error: " + url + "\n" + e);
    			
    		}
    	}
    _______________________________Attachment_____________________________
    Last edited by jscheuer1; 04-25-2009 at 02:54 PM. Reason: format code

  13. #9
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Hi again

    I've been looking at this and the only other thing that I can see wrong is that you're not passing the value of the input button to the function.

    With nothing in the brackets of the function, ie "()", then gpi_index is never set.

    Here is some code that will set gpi_index.

    JS - change to function - leave the rest from "method=" the same
    Code:
    function fire(gpi)
    {
    var gpi_index=gpi.value;
    HTML - I have also included a method to select using a drop down if you want to use that method.

    Code:
    <form action="index.php">
    <select name="gpi" onchange="fire(this)"/> <br />
    	<option></option>
    	<option value="1">1</option>
        <option value="2">2</option>
        <option value="5">5</option>
    </select>
        <br />
        <br />
    <input type="button" value="1" onclick="fire(this)" /><br />
    <input type="button" value="2" onclick="fire(this)" /><br />
    <input type="button" value="5" onclick="fire(this)" /><br />
    </form>
    Is "maxidrivers/maxisoftgpi" an application you have downloaded, because without that it is difficult to help further.

  14. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your function from post #8 in this thread:

    http://www.dynamicdrive.com/forums/s...98&postcount=8

    is still missing the closing brace.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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