简体   繁体   English

IIS Request.Form和JavaScript XMLHTTPRequest在Firefox和Chrome中不起作用

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

I'm using XMLHttpRequest to send FormData to an IIS server. 我正在使用XMLHttpRequest将FormData发送到IIS服务器。 On the server, the Request.Form object has form data when the browser is IE, but it has no form data when the browser is FireFox or Chrome. 在服务器上,当浏览器为IE时,Request.Form对象具有表单数据,但是当浏览器为FireFox或Chrome时,它没有表单数据。 The example below submits a form to the server using a FormData object. 下面的示例使用FormData对象将表单提交到服务器。 With IE, the Request.Form.Count is 1. With FireFox and Chrome it is zero. 对于IE,Request.Form.Count为1。对于FireFox和Chrome,它为零。 In all cases I would expect the result to be 1. I expect I'm doing something wrong, but I'm not seeing it. 在所有情况下,我都希望结果为1。我期望自己做错了什么,但我没有看到。

Here's the client code: 这是客户端代码:

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

And here's the server code: 这是服务器代码:

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

Any suggestions or explanations and solutions would be appreciated. 任何建议或解释和解决方案将不胜感激。

Turns out the problem was the setRequestHeader. 原来问题是setRequestHeader。 I removed it and got the desired behavior. 我将其删除并获得了所需的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM