/    Sign up×
Community /Pin to ProfileBookmark

post instead of get

Want to get the example below to work with open(“POST”,url,true) instead of open(“GET”,url,true).
When I try that, the program doesn’t seem to do anything when I click the buttons. The last try is commented out.
How do you get it to work with POST ?

[CODE]
<html>
<head>
<title>
</title>
<script language=”javascript” type=”text/javascript”>
<!–
function f(fn,dv)
{
var req=false;
if (window.XMLHttpRequest)
{
req=new XMLHttpRequest();
}
else
{
if (window.ActiveXObject)
{
req=new ActiveXObject(“Microsoft.XMLHTTP”);
}
};
if (req)
{
var fn1=fn+”?t=”+Math.random();
req.open(“GET”,fn1,true);
//req.open(“POST”,fn1,true);
//req.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);
req.onreadystatechange=function()
{
if (req.readyState==4 && req.status==200)
{
//var rt2=req.responseText;
var rt2=”<img src=”+req.responseText+”>”;
document.getElementById(dv).innerHTML=rt2;
delete req;
req=null;
}
};
req.send(null);
//req.send();
}
else
{
alert(‘No req.’);
return false;
};
}
–>
</script>
</head>
<body>
<div id=”dv1″>
<form>
<input type=”button” value=”*” onclick=”f(‘aa.txt’,’dv2′);”>
<input type=”submit” value=”*”>
</form>
</div>
<div id=”dv2″>
</div>
</body>
</html>
[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowJun 24.2014 — your req.send has to have the parameters passed inside it.

req.open('POST", fn, true);
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
req.onreadystatechange=function() {
if (req.readyState==4 &amp;&amp; req.status==200) {
document.getElementById(dv).innerHTML = '&lt;img src='+req.responseText+'&gt;';
delete req;
req = null;
}
};
req.send('t=' + Math.random());


You put it in the send, not in the URL.

Oh, and small tip, TRY to avoid making 'variables for nothing'.
Copy linkTweet thisAlerts:
@neginfauthorJun 25.2014 — Tried that and other things but no luck. Req.readyState went to 4 and req.status was 405.
Copy linkTweet thisAlerts:
@neginfauthorJun 25.2014 — Sorry, by accident reposted reply above.
Copy linkTweet thisAlerts:
@deathshadowJun 25.2014 — 405? Method not allowed?

Can you make a flat non-ajax method="post" form and have it work? Could be POST is blocked... could also be your server doesn't allow post and AJAX.

Though you're passing to a txt file, which makes no sense. "aa.txt" is not something you should have as a URL to a form handler. Apache could be blocking it. What is "aa.txt"? If you change it to a PHP file does it work?
Copy linkTweet thisAlerts:
@neginfauthorJun 25.2014 — The text file extension was the problem.

The example works with post method now.

Thank you very much.
×

Success!

Help @neginf 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...