简体   繁体   English

带有servlet的Ajax-我该如何管理doGet?

[英]Ajax with servlet - how can i manage to make doGet?

I have a function in Javascript which is checking my servlet: 我在Javascript中有一个函数正在检查我的servlet:

function validate() {
var idField = document.getElementById("name1");
var data = "name1=" + encodeURIComponent(idField.value);
if (typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "Validator"
req.open("POST", url, true);        
req.onreadystatechange = inserter
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   req.send(data);
}

function inserter() {
if (req.readyState == 4) {
    if (req.status == 200) {
        var msg1 = req.responseText
    if (msg1 == "") {
        document.getElementById("msg").innerHTML = "<div style=\"color:red\">Wadliwa nazwa</div>";
        document.getElementById("org").value = ''
    }
    else
        document.getElementById("org").value = msg1
    }
}

I have found out that: Instruction 我发现:说明

req.onreadystatechanged = inserter req.onreadystatechanged =插入器

sets the function responsible for handling server response. 设置负责处理服务器响应的功能。

Inserter function which dynamically changes the content of a page will be invoked asynchronously in case the server response arrives. 如果服务器响应到达,则将异步调用动态更改页面内容的插入器功能。

On my servlet I am getting the parameter from my website: 在我的servlet上,我从我的网站获取参数:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request.getParameter("name1"));
    if(!request.getParameter("name1").equals("")) {
        serviceConnection(request, response);
    }
}

Unfortunately when I try to do the same thing, but as the doGet, I can't do it at all...: 不幸的是,当我尝试做同样的事情,但是作为doGet时,我根本做不到...:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(request.getParameter("name2"));
    if(!request.getParameter("name2").equals("")) {
        serviceConnection(request, response);
    }
}

I was trying to change in the code of the ajax into: 我试图将ajax的代码更改为:

var url = "Validator/?" + data2;
req.open("GET", url, true);        
req.onreadystatechange = inserter2;

But it doesn't work. 但这是行不通的。 How can I solve this problem? 我怎么解决这个问题? Shall I do something other than 我可以做其他事情吗

req.onreadystatechanged = inserter to have a doGet function? req.onreadystatechanged =插入程序具有doGet函数?

Try with this code.. I corrected it cose you had misssing closing brackets, it should work now .. 尝试使用此代码。.我更正了它,因为您错过了右括号,它现在应该可以工作了..

function validate() {
    var idField = document.getElementById("name1");
    var data = "name1=" + encodeURIComponent(idField.value);
    if (typeof XMLHttpRequest != "undefined") {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "Validator"
    req.open("GET", url, true);
    req.onreadystatechange = inserter
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    req.send(data);
}


function inserter() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var msg1 = req.responseText
            if (msg1 == "") {
                document.getElementById("msg").innerHTML = "<div style=\"color:red\">Wadliwa nazwa</div>";
                document.getElementById("org").value = '';
            } else {
                document.getElementById("org").value = msg1;
            }
        }
    }
}

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

相关问题 如何在Servlet中的doGet和doPost方法之间传递局部变量? - How can I pass local variables between doGet and doPost method in Servlet? 如何使用doGet方法捕获从android应用发送到servlet的JSON对象? - How can I catch a JSON object sent from an android app to a servlet with doGet method? 为什么我的servlet代码中不能使用doGet方法? - Why can't i use doGet method in my servlet code? 我可以调用doGet方法并从servlet中检索信息吗? - Can I invoke the doGet method and retrieve information from a servlet? 使用Ajax从Servlet中的doGet获取参数 - Getting parameter from a doGet in Servlet using Ajax 如何在启动时调用Web应用程序中的Servlet(doGet)? - How to invoke a Servlet (doGet) in a web application on startup? 从Java Servlet方法doGet返回数据到ajax - Return data to ajax from java Servlet method doGet 通过AJAX调用将JSP页面与Servlet doGet()方法进行交互 - Interacting the JSP page with the Servlet doGet() method through AJAX call 如何在方法内部使用doGet响应? - how can I use doGet response inside a method? 如果我在HttpServlet #init(ServletConfig)中分配实例字段,Servlet规范是否保证我可以在doGet()中读取它们? - If I assign instance fields in HttpServlet#init(ServletConfig), does the Servlet spec guarantee I can read them in doGet()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM