服务端无法获取到Ajax发送post请求的参数

js类似于这样:

function send() {
            var xhr = new XMLHttpRequest();
            xhr.open("post", "AjaxTest.aspx", true);
            //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
                if (xhr.readyState===4) {
                    alert("接受到的文本是:"+xhr.responseText);
                }
            }
            xhr.send("name=" + document.getElementById("name").value);
        }

由于在发送请求的时候没有指定Content-Type,所以用无论时用Request.Form无法获得这些参数,因为Request.Form只会解析Content-Type 值为“application/x-www-form-urlencoded”或“multipart/form-data”时的参数。

但要想获得这些参数可以Request.InputStream得到:

if (Request.InputStream.Length>0)
            {
                StreamReader sr = new StreamReader(Request.InputStream);
                var requestStr = sr.ReadToEnd();
            }

参考http://bbs.csdn.net/topics/330125738

原文地址:https://www.cnblogs.com/rgshare/p/3159931.html