/    Sign up×
Community /Pin to ProfileBookmark

AJAX to Post or not a Get

I have sommen problems to get my AJAX Code to work whit “Post” i can get it to work to “GET” and here is my AJAX Code i hob i can be helpet her.

[code]
/* Will make the xmlHttp request to load the file ind the Firefox, Opera, Internet Explore and more browseres */
function makeRequestObj()
{
// Try Opera, Firefox, etc. browser method first
try
{
XMLHttpRec = new XMLHttpRequest();
}
catch (e)
{
// That method failed so try the Microsoft method
try
{
XMLHttpRec = new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e)
{
// OK – that failed, so try the older Microsoft ActiveX Object
try
{
XMLHttpRec = new ActiveXObject(“Microsoft.XMLHTTP”);
}
catch (e)
{
// Total failure – set XMLHttpRec to a boolean “false” value
XMLHttpRec = false;
}
}
}

// Pop up an alert box if there has been problems creating the object
if (!XMLHttpRec)
{
alert(“Error initializing XMLHttpRequest!”);
}

return (XMLHttpRec);
}

/*
GET GET GET GET GET GET GET GET GET GET

This code use to be call this file you whot to call

RequestObj = The Obj we definde to call a new xmlHttp request
url = The URL we load e.g test.htm
responseToId = Whit id tag on the pages we will change
*/
function xmlHttp(RequestObj, url, responseToId)
{
RequestObj.open(“GET”, url, true);

RequestObj.onreadystatechange = function ResponsToIdPage()
{
if (RequestObj.readyState == 4)
{
if (RequestObj.status == 200)
{
document.getElementById(responseToId).innerHTML = RequestObj.responseText;
}
}
}

RequestObj.send(null);
}

/*
POST POST POST POST POST POST POST POST

This code use to be call this file you whot to call

RequestObj = The Obj we definde to call a new xmlHttp request
url = The URL we load e.g test.htm
responseToId = Whit id tag on the pages we will change
*/
function DoPostRequest(RequestObj, url, responseToId)
{
RequestObj.open(“POST”, url,false);
RequestObj.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded’);
RequestObj.send(param);

if (RequestObj.status == 200)
{
return RequestObj.responseText;
}
else
{
return RequestObj.status;
}
}

Ajax_TestVar = makeRequestObj();
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@sridhar_423Sep 25.2006 — seems to be fine for me ..

is your "params" variable in the key value pair format.

e.g key1=value1&key2=value2&key3=value3
Copy linkTweet thisAlerts:
@NeoNmaNauthorSep 25.2006 — i dont understand so good whot you think... can you explain more ?
Copy linkTweet thisAlerts:
@sridhar_423Sep 25.2006 — i'm talking abt this variable
[CODE]RequestObj.send(param);[/CODE]

I doubt the format of the values tat you're posting using this variable as everything else seems to be fine.. should work
Copy linkTweet thisAlerts:
@sridhar_423Sep 25.2006 — param should be like ..

param="flag1=value&flag2=value2&flag3=value3";

in the post page(denoted by url tat you're passing to the function), the variables which are available in the request object are..

flag1, flag2 and flag3.

They'll have the values value1,value2 and value3 respectively.

clear
Copy linkTweet thisAlerts:
@NeoNmaNauthorSep 26.2006 — Tanks ? now i think ist work ?
Copy linkTweet thisAlerts:
@sridhar_423Sep 26.2006 — param should be like ..

param="flag1=value&flag2=value2&flag3=value3";[/QUOTE]


assuming tat you're not trying to pass some files, but just plain Strings.

Cheers,

Sridhar
×

Success!

Help @NeoNmaN spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...