0

I have number of values. I need to pass these values to another page without using the window.location

function sample(cID){
    var clg = ${param.clg};
    $.ajax({
        type : "post",
        url : "sampleShow?clg="+clg+"&cID="+cID+"&level="+level,
        dataType : "json",
        cache : false,
        beforeSend : function(xhr) {
            xhr.setRequestHeader("Accept", "application/json");
            xhr.setRequestHeader("Content-Type", "application/json");
        },
        success : function(response) {
            window.location= "cc"
        },
        error : function(xhr) {
            console.log("error"+xhr.status);
        },
        complete : function() {
        }
    });
}

This is my controller ajax function:

@RequestMapping(value = "sampleShow", method = RequestMethod.POST)
public @ResponseBody
String showc(HttpServletRequest request,Model  model)
{
    model.addAttribute("ccID", request.getParameter("cID"));
    model.addAttribute("clg", request.getParameter("clg"));
    model.addAttribute("level", request.getParameter("level"));

    return "{\"sucess\":\"true\"}";
}

I need to get the values in cc page.

cdbitesky
  • 1,292
  • 1
  • 13
  • 29
VSN
  • 41
  • 1
  • 7

1 Answers1

0

Your ajax call should like:

 $.ajax({
        type : "post",
        url : "sampleShow",            
        data :  {clg: clg, cID: cID, level: level },            
        success : function(response) {
            window.location= "cc"
        },
        error : function(xhr) {
            console.log("error"+xhr.status);
        },
        complete : function() {
        }
    });

No need to specify dataType: "json", as you are not sending json. Data is passed using 'data'. Refere jQuery.ajax() for more details.

You can also use shorthand method jQuery.post(), to post instead of jQuery.ajax()

You may also like to read on what-is-the-difference-between-post-and-get.

Community
  • 1
  • 1
Sudarshan_SMD
  • 2,426
  • 33
  • 21