...

View Full Version : Cannot get Ajax POST working



oneadvent
10-16-2008, 01:34 AM
I do not know where I went astray, if someone could point me in the right direction or point out where I have messed up I would be grateful.


<script language="JavaScript">
<!--
function ImageDelete() {
if (confirm("Are you sure you want to delete?") == false) {
return;
}
document.forms[0].ClearImage.value = '1';
document.forms[0].TransCondition.value = '4';
document.forms[0].submit();
return;
}
function ClickCancel() {
self.location = "http://68.157.251.100/CgiImageTransfer?Page=0&Language=0";
}
function ONStatus(){
document.forms[0].ConditionSave.value = '1';
document.forms[0].TaskEnable.checked = true
{
var xmlHttp;
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
}
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.ready&state==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("POST","http://oneadvent.viewnetcam.com:50050/CgiImageTransfer",true);
xmlHttp.send()
}



function OFFStatus(){
document.forms[0].ConditionSave.value = '1';
document.forms[0].TaskEnable.checked = false
{
var xmlHttp;
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
}
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.ready&state==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open('POST','http://oneadvent.viewnetcam.com:50050/CgiImageTransfer',false);
xmlHttp.send()
}
function ajaxFunction()
// -->
</script>


Thanks in advance!

A1ien51
10-16-2008, 02:54 AM
the xmlhttprequest object does not send the form data automatically, you need to specify it.

http://en.wikipedia.org/wiki/XMLHttpRequest

Eric

oneadvent
10-16-2008, 03:14 AM
So I should have:


var poststr = "Language=" + encodeURI( document.getElementById("Language").value ) +
"Page=" + encodeURI( document.getElementById("Page").value ) +
"TaskId=" + encodeURI( document.getElementById("TaskId").value );
xmlHttp.open('POST','http://XXXXXXXXXX.com:50050/CgiImageTransfer',false);


When my codes forms look like this:


<form method="post" action="http://XXXXXXXX.com:50050/CgiImageTransfer">
<input name="Language" value="0" type="hidden">
<input name="Page" value="1" type="hidden">
<input name="TaskId" value="2" type="hidden">

Of course, I shortened it considerably, but I'll do the grunt work, lol

A1ien51
10-16-2008, 04:47 AM
Or you could write a function that walks the form and builds the string instead of listing all of the elements. I believe people already posted that somewhere here on the forum.

OR

You can use a library that has it built in with all of the correct headers being set too.

Eric

oneadvent
10-16-2008, 04:54 AM
I like that walking thing if you have the code available,

Is that the right format for it to call the HttpRequest though?

ohgod
10-16-2008, 02:00 PM
if you're interested in a library as another option...

http://www.prototypejs.org/api/form/serialize

Basscyst
10-17-2008, 12:22 AM
//*******************************************************
// Builds a querystring off of the child form elements of the
// object passed to the function.
//*******************************************************
function formPost(top_element){
var inputs=top_element.getElementsByTagName('*');
var qstring=new Array();
for(var i=0;i<inputs.length;i++){
if(!inputs[i].disabled&&inputs[i].getAttribute('name')!=""&&inputs[i].getAttribute('name')){
qs_str=inputs[i].getAttribute('name')+"="+encodeURIComponent(inputs[i].value);
switch(inputs[i].tagName.toLowerCase()){
case "select":
if(inputs[i].getAttribute("multiple")){
var len2=inputs[i].length;
for(var j=0;j<len2;j++){
if(inputs[i].options[j].selected){
var targ=(inputs[i].options[j].value) ? inputs[i].options[j].value : inputs[i].options[j].text;
qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
}
}
}
else{
var targ=(inputs[i].options[inputs[i].selectedIndex].value) ? inputs[i].options[inputs[i].selectedIndex].value : inputs[i].options[inputs[i].selectedIndex].text
qstring[qstring.length]=inputs[i].getAttribute('name')+"="+encodeURIComponent(targ);
}
break;
case "textarea":
qstring[qstring.length]=qs_str;
break;
case "input":
switch(inputs[i].getAttribute("type").toLowerCase()){
case "radio":
if(inputs[i].checked){
qstring[qstring.length]=qs_str;
}
break;
case "checkbox":
if(inputs[i].value!=""){
if(inputs[i].checked){
qstring[qstring.length]=qs_str;
}
}
else{
if(inputs[i].checked){
qstring[qstring.length]=inputs[i].name+"=ON";
}
}
break;
default:
qstring[qstring.length]=qs_str;
break;
}
break;
}
}
}
return qstring.join("&");
}

oneadvent
10-17-2008, 04:25 PM
Thanks for that bit of code, I call it for the Post by:


document.forms[0].ConditionSave.value = '1';

document.forms[0].TaskEnable.checked = true
xmlhttp=new XMLHttpRequest();

xmlhttp.open("Post", 68.157.251.100/CgiImageTransfer, false);
xmlhttp.send(inputs);

document.forms[0].submit();

Thanks again for all your help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum