Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2013
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Send XML file in HTTP request

    Hi All,

    I am new to this HTML JavaScript development. I am working on one thing where I need to send HTTP POST request with XML fiile. That XML file doesn't exist on disc. I have multiple options on the HTML page and user can select multiple option to select. Each option has ID and I need to send this as XML file in HTTP Request. The content of XML file vary depending on the selection of options. Below is a sample XML content,

    <?xml version='1.0' encoding='UTF-8'?><OptionList><ID>1</ID><ID>2</ID></OptionList>

    How can I achieve this? Any reference sample, link, code would be very nice to understand.

    I appreciate your guidance in resolving this.

    Thanks,
    Jdp

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    ??? You want to *SEND* XML *from* the browser *to* the server? So you need JavaScript code that can create XML?

    Or do you mean the reverse: The browser *REQUESTS* XML from the server? Which is normal, ordinary, and indeed what the "X" in "AJAX" stands for. (Asynchronous Javascript And Xml)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Or do you mean that you don't want to use AJAX, at all? You just want the submit of a <form> to send data to the server in XML format?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    May 2013
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks Old Pedant for your reply,

    I want to send XML file to the Server. I have created a XML content that need to send but I don't know how to create XML file from it and then send a HTTP request.

    I appreciate your guidance.

    Thanks,
    Jdp

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    just search for "upload file javascript", that it's XML or text or image data doesn't really matter for the mechanics of this common request.

    these days, ajax is pretty potent: you can .send() just the string of the XML, a form with an input containing the source XML, the actual XML domDocument, or a virtual file added via FormData().

    each of those have slightly different catching routines on the server, but all can be prepared client-side with no more than 5 lines of code.
    Last edited by rnd me; 05-31-2013 at 10:05 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    New Coder
    Join Date
    May 2013
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks rnd me for your inputs,

    In my case I want to send XML file which need to be constructed dynamically. That XML file doesn't exist on disc.

    I would appreciate if you can point me to any link, article.

    Thanks,
    Jdp

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And in truth you could just send the XML as a string.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form id="theForm">
    <input type="checkbox" name="cid" value="1"/> 1
    <input type="checkbox" name="cid" value="2"/> 2
    <input type="checkbox" name="cid" value="3"/> 3
    <input type="checkbox" name="cid" value="18"/> 18
    <input type="checkbox" name="cid" value="73"/> 73
    <br/>
    <input type="button" name="doit" value="send"/>
    </form>
    
    <script type="javascript">
    var form = document.getElementById("theForm");
    form.doit.onclick = function( )
    {
        var cbs = form.cid;
        var list = "";
        for ( var c = 0; c < cbs.length; ++c )
        {
            var cb = cbs[c];
            if ( cb.checked ) { list += "<ID>" + cb.value + "</ID>"; }
        }
    
        var tosend = "<?xml version='1.0' encoding='UTF-8'?><OptionList>"
                   + list + "</OptionList>";
    
        var ajax =  (window.XMLHttpRequest != null )
                 ? new XMLHttpRequest()
                 : new ActiveXObject("Microsoft.XMLHTTP");
        ajax.open( "POST", "urlToPostTo.xxx", false ); // see comments
        ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        ajax.send( encodeURIComponent(tosend) );
        alert( ajax.responseText ); //or do something useful with it
    }
    </script>
    </body>
    </html>
    What I'm doing here is not truly AJAX. It's actually SJAX, where the "S" means "Synchronous". That is, I am letting the JS code *wait* for the response from the server to the POST request. If you are sending to your own server and you know it responds pretty speedily, that can work just fine. But you can also easily convert this to AJAX.

    Now, the question is: Just what will your server *DO* with the POSTed data? And will it really expect to get it just as a raw post?

    In ASP, your server code might be as simple as
    Code:
    <%
    xml = Request.Form
    ...
    %>
    And the variable xml will hold the XML text, as text, as sent.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    jdp12383 (05-31-2013)

  • #8
    New Coder
    Join Date
    May 2013
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Awesome, Thanks Old Pedant for your such a detail response.

    It just went in my mind directly. I was thinking that will be hard for me to understand this as a newbie. Your sample code has made the picture clear.

    I think I'll work on this now and come back if have any query.

    Thanks,
    Jdp


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •