0

I have a frameId varibale that I recovered from my javascript code and I want to pass on to my servlet, I use ajax for that but it did not work:

here is my javascript method:

function createYTEvent(frameID,j) {
    return function (event) {
        var player = playerArray[frameID];
        var duration=player.getDuration();

if(!videoArray[j].valide){
         if (event.data == YT.PlayerState.PAUSED) {
        if(((player.getCurrentTime()*100)/duration)>2){
                    videoArray[j].valide=true;
                $.ajax({
                     type: 'GET',
                     url: 'localhost:8080/favoris',
                     data: frameID
                     });
                    }
                }
         if (event.data == YT.PlayerState.ENDED) {
        videoArray[j].valide=true;
                alert("frame id"+frameID + "valide= "+ videoArray[j].valide);
                }
        }
    }
}

and here is my servlet: favoris.java

@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String n1 = request.getParameter("frameID");
        out.println(n1);


    } 

can you help me...

3 Answers 3

1

Based on how you're processing the get request on your server, it sounds like you want a named parameter in the ajax call that has the name "frameID". If that's the case, then the data line in your ajax call needs to change to this:

$.ajax({
     type: 'GET',
     url: 'localhost:8080/favoris',
     data: {"frameID": frameID}
});

This will put it into the URL as localhost:8080/favoris?frameID=xxx and then your server side code request.getParameter("frameID"); can fetch it from the URL parameters.


Also, in the future "did not work" isn't a very useful description for what happened which tends to make us have to guess what might be wrong. You should explain what you actually observed on the server and what debugging or diagnostic steps you took.

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you all for having responded to Abors me and I will consider your comments.

I put my servlet in a package (package1).

// when the player changes states
function createYTEvent(frameID,j) {
    return function (event) {
        var player = playerArray[frameID];
        var duration=player.getDuration();

if(!videoArray[j].valide){
         if (event.data == YT.PlayerState.PAUSED) {
        if(((player.getCurrentTime()*100)/duration)>2){
                    videoArray[j].valide=true;
                $.ajax({
                     type: 'GET',
                     url: 'localhost:8080/package1/favoris',
                     data: {"frameID": frameID}
                });
                    }
                }
         if (event.data == YT.PlayerState.ENDED) {
        videoArray[j].valide=true;
                alert("frame id"+frameID + "valide= "+ videoArray[j].valide);
                }
        }
    }
}

below the code for the doGet method of my servlet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
   response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    String n1 = request.getParameter("frameID");
    out.println(n1);
} 

The content of the variable frameId does not appear in the output, ie the passage does not always occur.

Comments

0

It is necessary to make a mapping (correspondence) between the servlet and the url like this::

@WebServlet(name = "favoris1")
public class favoris extends HttpServlet

for example if the project named "MyProject", url must be:

var url="/MonProjet/favoris1"

suddenly the ajax code becomes:

$.ajax({
                 type: 'GET',
                 url: '/exempleYoutube/favoris1?frameID='+frameID,
                 success: function(resp){
                    alert(resp);
                }
                 });

and the servlet:

@WebServlet(name = "favoris1")
public class favoris1 extends HttpServlet {
.
.
.
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        String n1 = request.getParameter("frameID");
        out.println("frameID= "+n1);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.