By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,267 Members | 531 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,267 IT Pros & Developers. It's quick & easy.

Not able to send data by POST using XMLHttpRequest (ajax)

P: 1

In my project i had a login form and i am trying to send the username and password to the server for authentication using xmlHttpRequest. Hence i am using POST request but i am unable to send data. I tried sending using GET request and GET is working fine.

Here is code snippet

Expand|Select|Wrap|Line Numbers
  1. function postRequest() { \n\
  2. if(window.ActiveXObject) \n\
  3. xmlHTTPObj = new ActiveXObject('Microsoft.XMLHTTP'); \n\
  4. else if (window.XMLHttpRequest) \n\
  5. xmlHTTPObj = new XMLHttpRequest(); \n\
  6. else xmlHTTPObj=null; \n\
  7. if(xmlHTTPObj) \n\
  8. { \n\
  9. if (document.unameform.UNAMEX.value != '') { \n\
  10. document.unameform.Authorization.value = document.unameform.UNAMEX.value; \n\
  11. document.unameform.Authorization.value += ':'; \n\
  12. document.unameform.Authorization.value += document.unameform.PWORDX.value; \n\
  13. var data='UNAMEX='; \n\
  14. data += document.unameform.UNAMEX.value; \n\
  15. data += '&PWORDX=' + document.unameform.PWORDX.value; \n\
  16. data += '&Authorization=' + document.unameform.Authorization.value; \n\
  17. xmlHTTPObj.onreadystatechange=processRequest; \n\
  18.'POST','START=FIRST',true); \n\ 
  19. xmlHTTPObj.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); \n\
  20. xmlHTTPObj.setRequestHeader('Content-length', data.length); \n\
  21. xmlHTTPObj.setRequestHeader('Connection', 'close'); \n\
  22. xmlHTTPObj.send(data); \n\
  23. } } } \n\
  24. function processRequest() { \n\
  25. if(xmlHTTPObj.readyState==4) { \n\
  26. if(xmlHTTPObj.status==200) { \n\
  27. alert('in process request'); \n\
  28. alert(xmlHTTPObj.responseText); \n\
  29. } } } \n\
  30. </script> \n\

As u can see in above code i am trying to send Authorization field for authentication. The sent message should look something like this


In above code if i change POST to GET then i am able to see following message sent


Thanks in advance
Sep 21 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 533
Hi ajaxcoder,

Try removing the setContentHeader("Connection") object, as (if I'm right) it's specifying the use of a closed connection which is impossible to retrieve data?
Also check your URL value "START=FIRST" - this should be the URL that you are trying to send data to but it appears to be a parameter that you want to send to the serving page?

Sep 21 '07 #2

Expert Mod 15k+
P: 16,027
ajaxcoder, welcome to TSDN!

Please remember to use code tags when posting code:

Javascript code goes here...
Sep 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.