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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajaxcoder
    New Member
    • Sep 2007
    • 1

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

    Hi

    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

    [CODE=javascript]function postRequest() { \n\
    if(window.Activ eXObject) \n\
    xmlHTTPObj = new ActiveXObject(' Microsoft.XMLHT TP'); \n\
    else if (window.XMLHttp Request) \n\
    xmlHTTPObj = new XMLHttpRequest( ); \n\
    else xmlHTTPObj=null ; \n\
    if(xmlHTTPObj) \n\
    { \n\
    if (document.uname form.UNAMEX.val ue != '') { \n\
    document.unamef orm.Authorizati on.value = document.unamef orm.UNAMEX.valu e; \n\
    document.unamef orm.Authorizati on.value += ':'; \n\
    document.unamef orm.Authorizati on.value += document.unamef orm.PWORDX.valu e; \n\
    var data='UNAMEX='; \n\
    data += document.unamef orm.UNAMEX.valu e; \n\
    data += '&PWORDX=' + document.unamef orm.PWORDX.valu e; \n\
    data += '&Authorization =' + document.unamef orm.Authorizati on.value; \n\
    xmlHTTPObj.onre adystatechange= processRequest; \n\
    xmlHTTPObj.open ('POST','START= FIRST',true); \n\
    xmlHTTPObj.setR equestHeader('C ontent-type', 'application/x-www-form-urlencoded'); \n\
    xmlHTTPObj.setR equestHeader('C ontent-length', data.length); \n\
    xmlHTTPObj.setR equestHeader('C onnection', 'close'); \n\
    xmlHTTPObj.send (data); \n\
    } } } \n\
    function processRequest( ) { \n\
    if(xmlHTTPObj.r eadyState==4) { \n\
    if(xmlHTTPObj.s tatus==200) { \n\
    alert('in process request'); \n\
    alert(xmlHTTPOb j.responseText) ; \n\
    } } } \n\
    </script> \n\
    [/CODE]



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

    POST /?START=FIRST HTTP/1.1
    .....
    .....
    Authorization=u sername[:]passwd


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

    GET /?START=FIRST HTTP/1.1


    Thanks in advance
    Last edited by acoder; Sep 21 '07, 11:27 AM. Reason: Added code tags
  • JamieHowarth0
    Recognized Expert Contributor
    • May 2007
    • 537

    #2
    Hi ajaxcoder,

    Try removing the setContentHeade r("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=FIRS T" - 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?

    medicineworker

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      ajaxcoder, welcome to TSDN!

      Please remember to use code tags when posting code:

      &#91;CODE=javas cript]
      Javascript code goes here...
      [/CODE]

      Comment

      Working...