如何使用 XMLhttpRequest 发送 html 代码

How to send html code with XMLhttpRequest

我使用此代码将信息发送到 NODEjs 服务器:

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

但现在我想发送一个 html 代码。示例:

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>';

我怎样才能做到这一点?现在我当然有一个格式错误的 URL.

我建议你使用 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));