PDA

View Full Version : Javascript newbie - POST request



rd42
04-24-2009, 03:16 PM
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 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>

rd42
04-24-2009, 03:22 PM
Oh yeah , the error message the "Error: Object Expected" refers to these lines:


<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.

jscheuer1
04-24-2009, 03:48 PM
Your function has no closing brace, add it here (highlighted red):


. . . 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.

forum_amnesiac
04-24-2009, 03:49 PM
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.

jscheuer1
04-24-2009, 03:51 PM
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.

rd42
04-24-2009, 04:44 PM
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.


http://fcgen3:8009/maxidrivers/maxisoftgpi/fire?gpi=1
http://fcgen3:8009/maxidrivers/maxisoftgpi/fire?gpi=2
http://fcgen3:8009/maxidrivers/maxisoftgpi/fire?gpi=3


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

forum_amnesiac
04-24-2009, 04:51 PM
I've opened the .txt file and there is nothing of value in it, are there more files you can post

rd42
04-24-2009, 06:19 PM
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/maxisoftgpi/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:


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_____________________________

forum_amnesiac
04-25-2009, 12:51 PM
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

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.


<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.

jscheuer1
04-25-2009, 03:03 PM
Your function from post #8 in this thread:

http://www.dynamicdrive.com/forums/showpost.php?p=194398&postcount=8

is still missing the closing brace.