如何从servlet页面中的ajax请求获取数据?

发布于 2024-11-07 19:01:04 字数 819 浏览 0 评论 0原文

如何从servlet页面中的ajax请求获取数据。这里我通过 $.ajax 调用 servlet。

var dataRequestObject= {}; 
                dataRequestObject= {Username:uname,Password:pword,Domain:domain,WindowsUser:windowsuser};

var dataRequestHeader= {}; 
                dataRequestHeader= {Username:uname,Password:pword,Domain:domain,WindowsUser:windowsuser};


    $.ajax({
            type:'POST',
            url:'http://localhost:8090/SampleServlet1/serv', //calling servlet      
            cache:false,
            header:dataRequestHeader,
            data:dataRequestObject,
            success:function(){ alert("Request Done");},
            error:function(xhr,ajaxOptions){
                alert(xhr.status + " :: " + xhr.statusText);
                } 
            });

我需要 setvlet 页面中的这些数据和标题。

How to get the data from ajax request in servlet page. Here I'm calling the servlet by $.ajax.

var dataRequestObject= {}; 
                dataRequestObject= {Username:uname,Password:pword,Domain:domain,WindowsUser:windowsuser};

var dataRequestHeader= {}; 
                dataRequestHeader= {Username:uname,Password:pword,Domain:domain,WindowsUser:windowsuser};


    $.ajax({
            type:'POST',
            url:'http://localhost:8090/SampleServlet1/serv', //calling servlet      
            cache:false,
            header:dataRequestHeader,
            data:dataRequestObject,
            success:function(){ alert("Request Done");},
            error:function(xhr,ajaxOptions){
                alert(xhr.status + " :: " + xhr.statusText);
                } 
            });

I need those data and header in setvlet page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2024-11-14 19:01:04

首先,header 选项无效。它必须是标题。另请参阅 jQuery.ajax() 文档

您可以使用 HttpServletRequest#getHeader()

String headerUsername = request.getHeader("Username");
String headerPassword = request.getHeader("Password");
String headerDomain = request.getHeader("Domain");
String headerWindowsUser = request.getHeader("WindowsUser");
// ...

您可以使用 HttpServletRequest#getParameter()

String paramUsername = request.getParameter("Username");
String paramPassword = request.getParameter("Password");
String paramDomain = request.getParameter("Domain");
String paramWindowsUser = request.getParameter("WindowsUser");
// ...

然而,我想知道通过标题和参数复制数据有多么有用......

First, the header option is invalid. It needs to be headers. See also the jQuery.ajax() documentation.

You can get the individual request headers by their name using HttpServletRequest#getHeader().

String headerUsername = request.getHeader("Username");
String headerPassword = request.getHeader("Password");
String headerDomain = request.getHeader("Domain");
String headerWindowsUser = request.getHeader("WindowsUser");
// ...

You can get the individual request parameters by their name using HttpServletRequest#getParameter().

String paramUsername = request.getParameter("Username");
String paramPassword = request.getParameter("Password");
String paramDomain = request.getParameter("Domain");
String paramWindowsUser = request.getParameter("WindowsUser");
// ...

I however wonder how useful it is to duplicate the data over headers and parameters...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文