How to pass parameters without using url

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.

Answers


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.


Need Your Help

UnityRegistry.Scan vs. UnityContainer.RegisterTypes

c# unity-container

I have an application that uses Unity. In one area of the application, the code scans the assembly to register the types (auto-registration) using this code:

Fill external form with cURL

php forms post curl

I'm trying to fill and submit an external form using cURL, but actually I can't fill the field and I would understand why.