【问题标题】:How do you sent HTTP POST method using Ajax and retrieve the input field values without Jquery or prototype?您如何使用 Ajax 发送 HTTP POST 方法并在没有 Jquery 或原型的情况下检索输入字段值?
【发布时间】:2010-12-16 06:37:21
【问题描述】:

我认为我已经非常接近解决方案,但无法真正弄清楚如何使用 Ajax 发送 HTTP POST 方法。我认为这也将有助于我了解我的 REST 项目。

网上有很多使用 Jquery 或 Prototype js 框架的资源,但我想知道没有它怎么办。

 <script type="text/javascript">
        function loadXMLDoc() {
            var xmlhttp = new XMLHttpRequest();
            var out;

            xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    out = xmlhttp.responseText;
                    alert(out);
                }
            }
            xmlhttp.open("POST", "/resources/Inventory/2", true);
            xmlhttp.setRequestHeader("Content-type", "application/jason");
            xmlhttp.setRequestHeader("Connection", "close");

            xmlhttp.send(null);
        }

        </script>

当使用 non-ajax POST(即页面重新加载或重定向)时,在服务器端,我可以获取用户提交的 HTML 表单的输入值。

但我的困惑在于 Ajax HTTP POST,我如何获取 HTML 表单的输入字段中指定的值? 是否有可能或不是正确的思维方式?

<form method="post" action="">
   <input type="text" name="info1" />
   <button value="click to call js" onclick="loadXMLDoc()">
</form>

【问题讨论】:

    标签: javascript ajax http-post


    【解决方案1】:

    您需要在请求正文中发送 POST 数据,例如"field1=value1&field2=value2"。

    查看这里了解更多信息:http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

    检查那里的内容类型!

    【讨论】:

      猜你喜欢
      • 2018-11-05
      • 2017-09-15
      • 2015-11-06
      • 2011-09-30
      • 1970-01-01
      • 2012-04-08
      • 2011-02-26
      • 2021-06-12
      • 2020-08-11
      相关资源
      最近更新 更多