如何从客户端向HttpContext.Request.Form添加参数

时间:2017-03-21 21:40:34

标签: javascript jquery asp.net asp.net-mvc

我想知道如何通过客户端将参数添加到HttpContext.Request.Form中,以便在服务端我可以获取这些数据 我不想使用ajax。 我尝试了以下但没有成功:

javascript代码:

var request = new XMLHttpRequest();

    request.open("POST", window.location.host, true);  
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");        
    var formData = new FormData();
    formData.append('key1', 'value1');
    formData.append('key2', 'value2');
    formData.append('skip', '10');
    request.send(formData);

asp.net mvc代码行:

var a = HttpContext.Request.Form.GetValues("skip");

但是a等于null。

谢谢大家

更新: 我想做像datatable这样的事情。在数据表中,您可以设置draw,start,col_order等。您可以通过请求进入服务器端。我想知道我该怎么办。

1 个答案:

答案 0 :(得分:1)

您需要合并这样的数据 - <button type="button" onclick="postData()">Post data</button> <div id="result"></div> <script> function postData() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { document.getElementById("result").innerHTML = this.responseText; } }; xhttp.open("POST", "@Url.Action("PostData", "Home")", true); xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); xhttp.send("key1=value1&key2=value2&skip=10"); } </script>

视图

public class HomeController : Controller
{
    public ActionResult Index()
    {    
        return View();
    }

    [HttpPost]
    public JsonResult PostData(FormCollection collection)
    {
        var key1 = collection["key1"];
        var key2 = collection["key2"];
        var skip = collection["skip"];

        return Json($"key1: {key1}, key2: {key2}, skip: {skip}");
    }
}

控制器

from scapy.all import *
def le_check(p):
    return (DNS in p and p[0][DNSQR].qtype == 1 and p[0][UDP].dport==53)

def main():
     data_base = {'www.google.com': ('172.217.23.164','173.194.79.104','173.194.79.99')}

     p1=sniff(count=1, lfilter=le_check)



     x=p1[0][DNSQR].qname
     if x in data_base:
        list=data_base[x]
     else:
         list= "no such name"
     print p1[0][DNSQR].qname
     print list
     send(IP(dst=p1[0][IP].src)/UDP(sport=53, dport=53)/Raw(list))

     if __name__ == '__main__':
     main()

屏幕截图

enter image description here

enter image description here