简体   繁体   English

从Ajax调用Servlet

[英]Calling a Servlet from Ajax

I have a Java servlet, that i need to call and pass it a variable using Ajax. 我有一个Java Servlet,我需要使用Ajax调用并将其传递给变量。 I have written an Ajax script, to get the variable that needs to be passed to the servlet. 我已经编写了一个Ajax脚本,以获取需要传递给servlet的变量。 However i am not sure how to do so. 但是我不确定该怎么做。 Any help on this matter please? 在这件事上有什么帮助吗? This is my ajax code: 这是我的ajax代码:

var data;
    data = "NUMBER ='" + Number + "'";

    var Key = '';
    $.ajax({
        type: "POST",
        url: "Record?DB=EMP&Table=EMP_HISTORY&",
        dataType: 'xml',
        data: {
            "Where": data
        },
        success: function(xml) {
            $(xml).find('record').each(function() {
                key  = $(this).find("PK").text();
            });
        },
        error: function(error) {
        }
    });

Your url parameter has & at last, I don't know if you have done it purposefully. 您的url参数最后有&,我不知道您是否故意这样做。 However you may try this : 但是,您可以尝试以下操作:

$.ajax({

                    url:"Record?DB=EMP&Table=EMP_HISTORY",
                    data:{Where:data},
                    contentType:"application/json; charset=utf-8",
                    dataType:"json",
                    success: function(xml) {
                      $(xml).find('record').each(function() {
                         key  = $(this).find("PK").text();
                      });
                    },
                    error:function () {

                    }
        }); 

It's unclear that which step ur in.Since that ,i would rather give u some advice. 目前尚不清楚该进入哪一步。因此,我希望向您提供一些建议。

1、if u dont use any webframework, then goto file web.xml and edit the servlet tag.configure the url and the according serlvet.Then u can overwrite the doPost() method in the servlet and receive the http request. 1,如果您不使用任何Web框架,则转到文件web.xml并编辑servlet标记。配置url和相应的serlvet。然后您可以覆盖servlet中的doPost()方法并接收http请求。

2、if u use webframework like struts.u can modify the configuration in struts.xml and write the according method in ur action to deal with the request. 2,如果您使用像struts.u这样的webframework,可以在struts.xml中修改配置,并在您的操作中编写相应的方法来处理请求。

3、if u use jsp as ur solution.u can simple do it in the jsp file. 3,如果您使用jsp作为您的解决方案,您可以在jsp文件中简单地进行操作。 Deal with the request variables through getRequestParameter and out.print the result. 通过getRequestParameter处理请求变量并输出结果。

hope my advice is helpful! 希望我的建议对您有所帮助!

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

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