1

I need to pass both FormData and JSON object in an ajax call, but I get a 400 Bad Request error.

[artifact:mvn]  org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn]  at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn]  at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]

JS:

var formData = new FormData(form[0]);
//form JSON object
var jsonData = JSON.stringify(jcArray);
$.ajax({
        type: "POST",
        url: postDataUrl,
        data: { formData:formData,jsonData:jsonData },
        processData: false,
        contentType: false,
        async: false,
        cache: false,
        headers: { 
            'Accept': 'application/json',
            'Content-Type': 'application/json' 
        },
        success: function(data, textStatus, jqXHR) {
        }
});

Controller:

@RequestMapping(value="/processSaveItem",method = RequestMethod.POST")
public @ResponseBody Map<String,String> processSaveItem(
                                  @RequestBody XYZClass result[])

}

There is similar question, jquery sending form data and a json object in an ajax call and I'm trying the same way.

How can I send both FormData and JSON object in a single ajax request?

3 Answers 3

0

You can pass in this way

   postdata={};
   postdata.formData=formData;
   postData.jsonData=jsonData
 $.ajax({
    type: "POST",
    url: postDataUrl,
    data: { postdata},
    processData: false,
    contentType: false,
    async: false,
    cache: false,
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    success: function(data, textStatus, jqXHR) {
    }

});

and controller side you can identify the data.

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

Comments

0

You can do the following to pass form data in Ajax call.

var formData = $('#client-form').serialize();
$.ajax({
    url: 'www.xyz.com/index.php?' + formData,
    type: 'POST',
    data:{
    },
    success: function(data){},
    error: function(data){},
})

4 Comments

lol. This is not what I wanted. How does this answer my question and Where is the json data in your answer?
In data section you can pass json object with reference and form data is being appended in URL section. For example, data:{jsonObject: JsonObject },
You didn't understand my question properly. I need to send both json object and formData together. Your response implies that you are sending only one of them.
Here I am sending my form data also. I am passing my form data in Url.
0

I solved it by appending the JSON string with FormData object in ajax POST.

var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);

and in controller you can access it the normal way as you do for other form data values.

request.getParameter("jsonObjectData");

and now you'll be having the Stringified JSON and you can parse the json to Java object

How to parse a JSON string to an array using Jackson.

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.