0

I have a Java servlet, that i need to call and pass it a variable using Ajax. I have written an Ajax script, to get the variable that needs to be passed to the servlet. However i am not sure how to do so. Any help on this matter please? This is my ajax code:

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) {
        }
    });
mikeb
  • 709
  • 2
  • 9
  • 35
  • Firstly, check the browser console for any errors. – JRodDynamite Jan 11 '16 at 05:20
  • @JasonEstibeiro dear, i have no errors, but i'm not yet calling the servlet – mikeb Jan 11 '16 at 05:22
  • so, what's the problem - the ajax code looks valid enough, does it work? only you can answer that – Jaromanda X Jan 11 '16 at 05:23
  • @JaromandaX i entend on calling a servlet : Form.java from this ajax and passing it the variable data. How can it be done? – mikeb Jan 11 '16 at 05:24
  • 1
    your question is getting less clear with every comment you post!! what, if anything, have you tried to do? – Jaromanda X Jan 11 '16 at 05:26
  • @mikeb - If you google "calling servlet using ajax", you'll surely get a lot of tutorial/blogs on this topic. Have a look at it. If you are facing a specific issue/error then post it here in the question. – JRodDynamite Jan 11 '16 at 05:28
  • @JaromandaX dear it's getting less clear because i have never done this before. In the Ajax code i'm running a condition to get the PK (the variable data)of certain records in my database. On the other hand i have a Java Servlet, that i need to pass this variable(data) to using the Ajax code. Getting any clear? – mikeb Jan 11 '16 at 05:30
  • are you asking for javascript code or java code, or some other language code – Jaromanda X Jan 11 '16 at 05:31
  • @JaromandaX my servlet is done. I'm just asking what's the proper way, to call it from my ajax code and pass it my variable: data – mikeb Jan 11 '16 at 05:32
  • Try reading this: http://stackoverflow.com/questions/3028490/calling-a-java-servlet-from-javascript – Sean Glover Jan 11 '16 at 05:35
  • @SeanGlover thank you dear!! – mikeb Jan 11 '16 at 05:37

2 Answers2

0

Your url parameter has & at last, I don't know if you have done it purposefully. 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 () {

                    }
        }); 
Joydeep
  • 76
  • 9
0

It's unclear that which step u r 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.

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.

3、if u use jsp as ur solution.u can simple do it in the jsp file. Deal with the request variables through getRequestParameter and out.print the result.

hope my advice is helpful!

herokingsley
  • 403
  • 3
  • 10