1

Hi I need to pass the full model and one string from html to Spring controller using AJAX. I use the below code snippet but it doesn't work.

var str = $("#resourceManagement").serialize();
        var agreementId = ${agreementId};
        var tempCnltName=$modal.find("input[data-type='cnltName']").val();
        $.ajax({
            type:"POST",
            data: {str, tempCnltName}, 
            url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline",
            async: false,
            dataType: "json",
            success: function (data, status, xhr) {

                message =  data.errorMsg;
            },
            error: function () {

            }
        });

The problem is that if I pass model alone (str) or string alone (tempCnltName) I can get it in controller but I cannot get both together.

My controller is as below:

@RequestMapping(value = "/app/agreement/checkDuplicateConsultantOnline", method = RequestMethod.POST)
public @ResponseBody AjaxResponse checkDuplicateConsultantOnline(
        @ModelAttribute("consultantBidModel") ConsultantBidModel model,
        @RequestParam(value = "tempCnltName", required = false) String cnltName,
        HttpServletRequest request,
        HttpSession session) throws Exception {

    final Set<String> allCnltNames = new HashSet<>();
    String errMessage = "";
    if (model.getLeadingCnltName() != null) {
        allCnltNames.add(model.getLeadingCnltName().toLowerCase());
    }
    if (model.getJointVentureConsultants() != null) {
        for (ConsultantBidListItem entry : model.getJointVentureConsultants()) {
            if (!allCnltNames.add(entry.getCnltName().toLowerCase())) {
                errMessage = "Each consultant can only appear once.";
            }
        }
    }
    if (model.getSubConsultants() != null) {
        for (ConsultantBidListItem entry : model.getSubConsultants()) {
            if (!allCnltNames.add(entry.getCnltName().toLowerCase())) {
                errMessage = "Each consultant can only appear once.";
            }
        }
    }
    AjaxResponse response = new AjaxResponse();
    if (errMessage != null) {
        response.setSuccess(true);
        response.setResponseObject(errMessage);
        response.setErrorMsg(errMessage);
    }
    return response;
}

3 Answers 3

1

On the server side, you're already prepared to receive both the model (with @ModelAttribute) and an additional URL parameter (with @RequestParam)

On the client, append the URL parameter to the URL. Assuming that str is your model and tempCnltName is your string to submit to the server:

$.ajax({
    type:"POST",
    data: str,
    url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline?tempCnltName=" + tempCnltName,
...
Sign up to request clarification or add additional context in comments.

Comments

0

try

var strVal = $("#resourceManagement").serialize();
    var agreementId = ${agreementId};
    var tempCnltNameVal=$modal.find("input[data-type='cnltName']").val();
    $.ajax({
        type:"POST",
        data: {str: strVal, tempCnltName: tempCnltNameVal}, 
        url: "${AGREEMENT_BASE_URL}/checkDuplicateConsultantOnline",
        async: false,
        dataType: "json",
        success: function (data, status, xhr) {

            message =  data.errorMsg;
        },
        error: function () {

        }
    });

Probably the malformed json is causing trouble

1 Comment

Thx for help but this doesn't work. I just updated my post to show my controller code. I think strVal doesn't map to my model.
0

Another way of doing the above, add the string to model:

var strVal = "consulName=" + tempCnltName + "&";strVal = strVal + $("#resourceManagement").serialize();

The model can then have a new parameter consulName and we can get the value in Controller.

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.