0

This is what I am trying to do. On a home page.. say /home.jsp, a user clicks on a link. I read value of the link and on the basis of which I call a RESTful resource which in turn manipulates database and returns a response. Interaction with REST as expected happens with use of JavaScript. I have been able to get information from REST resource but now I want to send that data to another JSP.. say /info.jsp. I am unable to do this.

I was trying to make another ajax call within success function of parent Ajax call but nothing is happening. For example:

function dealInfo(aparameter){



    var requestData = {
            "dataType":    "json",
            "type"    :    "GET",
            "url"     :    REST resource URL+aparameter,
    };

    var request = $.ajax(requestData);
    request.success(function(data){

        alert(something from data); //this is a success

        //I cannot get into the below AJAX call
        $.ajax({

            "type": "post",
            "url": "info.jsp"
            success: function(data){
                alert("here");
                ("#someDiv").html(data[0].deviceModel);
            }

        });

How do I go about achieving this? Should I use some other approach rather than two Ajax calls? Any help is appreciated. Thank You.

2 Answers 2

1

You can use the following function:

function dealInfo(aparameter) {
    $.ajax({
        url: 'thePage.jsp',
        type: "GET",
        cache: false,
        dataType: 'json',
        data: {'aparameter': aparameter},
        success: function (data) {

            alert(data); //or you can use console.log(data);

            $.ajax({
                url: 'info.jsp',
                type: "POST",
                cache: false,
                data: {'oldValorFromFirstAjaxCall': data},
                success: function (info) {
                    alert(info); //or you can use console.log(info);
                    $("#someDiv").html(info);
                }
            });

        }
    });
}

Or make the AJAX call synchronous:

function dealInfo(aparameter) {
    var request = $.ajax({
        async: false, //It's very important
        cache: false,
        url: 'thePage.jsp',
        type: "GET",
        dataType: 'json',
        data: {'aparameter': aparameter}
    }).responseText;

    $.ajax({
        url: 'info.jsp',
        type: "POST",
        cache: false,
        data: {'oldValorFromFirstAjaxCall': request},
        success: function (info) {
            alert(info); //or you can use console.log(info);
            $("#someDiv").html(info);
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

In this way I'm using.

"type": "post" instead of type: 'post'


Maybe it will help. Try it please. For Example;

$.ajax({
      url: "yourURL",
      type: 'GET',
      data: form_data,
      success: function (data) {
      ...
      }
});

1 Comment

Sorry it didn't work. It is not reaching the JSP I think.

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.