IIS的请求.表单和JavaScript XMLHTTPRequest不工作在Firefox和Chrome

IIS Request.Form and JavaScript XMLHTTPRequest not working in Firefox and Chrome

本文关键字:工作 Firefox Chrome JavaScript 请求 表单 IIS XMLHTTPRequest      更新时间:2023-09-26

我使用XMLHttpRequest发送FormData到IIS服务器。在服务器端,请求。当浏览器是IE时,表单对象具有表单数据,但当浏览器是FireFox或Chrome时,它没有表单数据。下面的示例使用FormData对象向服务器提交表单。在IE中,Request.Form.Count是1。而火狐和Chrome则是零。在所有情况下,我都希望结果是1。我想我做错了什么,但我没有发现。

下面是客户端代码:

<script>
    function SubmitForm() {
        var http = new XMLHttpRequest()
        var data = new FormData()
        data.append("Text1", document.getElementById("Text1").textContent);
        http.open("POST", "ajaxtest.aspx", true);
        http.setRequestHeader("Content-Type", "multipart/form-data");
        http.onreadystatechange = function () {
            if (http.readyState == 4 && http.status == 200) {
                alert(http.responseText);
            }
        }
        http.send(data);
    }
</script>
<body>
    <input id="Text1" type="text" value="This is text to the server" />
    <input id="Button1" type="button" value="button" onclick="SubmitForm()" />
</body>

服务器代码:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Request.HttpMethod = "POST" Then
        Response.Write("Request.Form.Count=" & Request.Form.Count)
        Response.End()
    End If
End Sub

原来问题是setRequestHeader。我删除了它,得到了想要的效果