0

I want to submit my jsp using jquery ajax form submit. While submitting browser showing "Content type 'application/json;charset=utf-8' not supported"

   var form = $('#encryptForm');
            $(function() {
               $('button[type=submit]').click(function(e) {
                  //Prevent default submission of form
                  e.preventDefault();

                  $.post({
                     url : 'encrypt',
                     dataType: "json",
                     contentType: "application/json; charset=utf-8",
                     data : form.serialize(),
                     success : function(res) {
                         console.log(res)
                     }
                  })
               });
            });



  <form id="encryptForm" name="encryptForm" method="POST">
                <fieldset>
                    <legend>User Fields</legend>
                    <p>
                        <textarea rows="4" cols="50" id="encryptData" >{"mid":"MID-NehoT","message":"hello"}</textarea>
                    </p>
                    <p>
                        <button type="submit">Submit</button>
                    </p>
                </fieldset>
            </form>



       @RequestMapping(value = "/encrypt", method = RequestMethod.POST)
        public Response post(@RequestBody Request request) {

            return null;
        }



@JsonIgnoreProperties(ignoreUnknown = true)
public class Request {
    private String mid;
    private String message;



    @JsonCreator
    public Request(String mid, String message) {
        this.mid = mid;
        this.message = message;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

3 Answers 3

1

You are defining dataType: "json", but passing form.serialize() which is not JSON

Pass JSON like this

$('button[type=submit]').click(function(e) {
  //Prevent default submission of form
  e.preventDefault();
  var json = {"mid":"MID-NehoT","message":"hello"}
  $.post({
     url : 'encrypt',
     dataType: "json",
     contentType: "application/json; charset=utf-8",
     data : JSON.stringify(json),
     success : function(res) {
         console.log(res)
     }
  })
});

I also assume your Request object contains field mid and message

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

3 Comments

Also pls add Request class in Question
Request class added
1

Either remove @RequestBody from your code like following:

@RequestMapping(value = "/encrypt", method = RequestMethod.POST)
public Response post() {
    return null;
}

or use Map instead like following:

@RequestMapping(value = "/encrypt", method = RequestMethod.POST)
public Response post(@RequestBody Map<String, Object> requestData) {
   return null;
}

1 Comment

No I want to post with the object.
0
  $.post({
     url : 'encrypt',
     dataType: "json",
     contentType: "application/json; charset=utf-8",
     data : $('#txtEn').val(),
     success : function(res) {
         console.log(res)
     }
  })

i just passed as normal string.

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.