2

I use this code to send information to a NODEjs server :

var obj=new XMLHttpRequest();
vat txt2send = "test";
obj.open("GET","http://localhost:1234" + "?variable=" + txt2send, true);
obj.send(null);

But now i would like to send an html code. Example :

var txt2send = '<div class="generate_div_last">
            <h3>Sentences:</h3>
            <form id="sent" name="sent" method="post" action="http://www.ipsum-generator.com/loremipsum/sentences">
                <input type="text" id="numsent" name="num" size="2" maxlength="3" value="50" />
                <input type="submit" id="poslji3" class="poslji" name="poslji" value="Generate!" />
                <h4>Options:</h4>
                <input type="checkbox" id="p_tags_sent" name="p_tags" />
                    <span class="tiny2">
                        <label for="p_tags_sent">Add paragraph tags (&lt;p&gt;)</label>
                    </span><br />
                <input type="checkbox" id="i_tags_sent" name="i_tags" />
                    <span class="tiny2">
                        <label for="i_tags_sent">Add <i>italic</i> tags (&lt;i&gt;)</label>
                    </span><br />
                <input type="checkbox" id="b_tags_sent" name="b_tags" />
                    <span class="tiny2">
                        <label for="b_tags_sent">Add <b>bold</b> tags (&lt;b&gt;)</label>
                    </span><br />
            </form>
            <div class="small">This option will generate the specified number of sentences of lorem ipsum.</div>
        </div>';

How can i achieve that ? Now of course i have a malformed URL.

bob dylan
  • 989
  • 2
  • 10
  • 26

1 Answers1

2

I recomend to you use post not get for this case.

data = {
   variable1 : ...,
   variable2 : ...
}

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("POST", "/yoururl");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(data));
Raúl Martín
  • 4,471
  • 3
  • 23
  • 42
  • that seems great, but it's my first time with NODEjs, and i currently don't know how to capture POST variables. (i use `var params=url.parse(request.url,true).query` to capture GET from the url) – bob dylan Aug 10 '15 at 09:38
  • ;) http://stackoverflow.com/questions/4295782/how-do-you-extract-post-data-in-node-js – Raúl Martín Aug 10 '15 at 09:41
  • another stupid question, if `console.log(post)` give me `{ '{"var1":"the text"}': '' }` How can i have "the text" ? – bob dylan Aug 10 '15 at 10:43
  • `post = JSON.parse(post)` but, tell me,where you get post... It's strange that it is as string. – Raúl Martín Aug 10 '15 at 10:51
  • this strange syntax come from `var qs=require("querystring")` and `qs.parse` but thanks again `JSON.parse` is much better :D – bob dylan Aug 10 '15 at 11:52