Forum Moderators: open

Message Too Old, No Replies

How to POST the AJAX request

Sorry.. newbie question, I guess

         

FourDegreez

2:16 am on Sep 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm new to AJAX. On some of my pages I want to have a form for submitting feedback. Relevant code snippet is below for using a GET request. Variable myurl contains the url with fields in the query string, and this works. However I want to do the same thing only with a POST and to submit the fields that way rather than in the URL.

xmlhttp.open('GET', myurl, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
var content = xmlhttp.responseText;
if ( content!= 'err' )
document.getElementById('fbbox').innerHTML = content;
else
document.getElementById('fbbox').innerHTML = 'There was an error';
}
}

How might I do a POST rather than a GET, and set the fields?

daveVk

2:37 am on Sep 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function makeReq( iUrlS, iDataS, iReplyHandle ){
if ( iDataS == '' ) {
gHttp.open( 'get', iUrlS );
}
else {
gHttp.open( 'post', iUrlS );
gHttp.setRequestHeader( 'Content-Type'
, 'application/x-www-form-urlencoded' );
}
gHttp.onreadystatechange = iReplyHandle;
if ( iDataS == '' ) { gHttp.send(null); }
else { gHttp.send( iDataS ); }
}

This is the code I have been using, does 'get' or 'post', iDataS are the arguments to be posted NOT part of iUrlS

cmarshall

12:00 pm on Sep 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dave's will work.

I have a far more comprehensive solution, but I'm not allowed to post a link to it here, and it's too much code to post.

It's basically a full "AJAX Queue Class", handling things like pipelining and selecting the HTTPRequest phase of the callback.

One thing that I do is take a full URI, with the standard question mark/ampersand format, and make a POST query from it, if that is what you want. The calling context doesn't have to worry about doing anything more than setting a flag. I'm pretty sure I took the technique directly from some samples out there.

[edited by: DrDoc at 10:26 pm (utc) on Sep. 23, 2006]
[edit reason] TOS #13 [/edit]

ahmedtheking

2:43 pm on Sep 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Tell you what, have a look at the Prototype JavaScript Framework [prototype.conio.net]. It really helps with dev'ing!

[edited by: DrDoc at 11:05 pm (utc) on Sep. 23, 2006]
[edit reason] properly linked [/edit]

cmarshall

3:56 pm on Sep 23, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks! I'll check it out.