weixin_33719619 2015-05-24 15:38 采纳率: 0%
浏览 31

使用AJAX进行GET和POST

So Im new to web programing, I need to GET an HTML page, containing a Form that I want to send via POST, this is what I tried so far:

<script>
var msgpost=new XMLHttpRequest();
msgpost.open("GET","http://localhost/elgg/pg/messages/compose",false);
msgpost.send();
alert("fine");
document.addEventListener("DOMContentLoaded", function(){
    alert("fine2");
    var hidden1=document.getElementByName("__elgg_token").value;
    var hidden2=document.getElementByName("__elgg_ts").value;
    alert("fine3");
    alert("sending hidden1 "+hidden1+" 2 "+hidden2);
    msgpost.open("POST","http://localhost/elgg/action/messages/send",true);
    msgpost.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    msgpost.send("__elgg_token="+hidden1+"&__elgg_ts="+hidden2+"&message=PWND&send_to=9&title=PWND");
    alert("done.");
},false);
</script>

"__elgg_token" and "__elgg_ts" are hidden input fields that I need to get their value to POST to the server from /compose. So I get "fine" and "fine2", the rest is not working and FireBug isn't showing any POST messages outgoing. Any Ideas?

  • 写回答

1条回答 默认 最新

  • weixin_33744141 2015-05-24 15:55
    关注

    I think you should use

    var hidden1 = document.getElementsByName("__elgg_token").value;
    var hidden2 = document.getElementsByName("__elgg_ts").value;
    
    评论

报告相同问题?