简体   繁体   中英

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

I'm using XMLHttpRequest to send FormData to an IIS server. 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. The example below submits a form to the server using a FormData object. With IE, the Request.Form.Count is 1. With FireFox and Chrome it is zero. In all cases I would expect the result to be 1. I expect I'm doing something wrong, but I'm not seeing it.

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. I removed it and got the desired behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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