0

I'm making a post method using $.ajax which is this

$(function(){

     $("#postMembers").click(function(){
            let member ={
                firstName: "khookina",
                lastName : "khooak",
                age:1,
                sex:"female",
                duty:"undefined",
                dailyJeton:2
            }

            $.ajax({
                type: "post",
                url: "http://localhost:8080/RCP2/members",
                data: member,
                dataType: "application/json",
                success: function (response) {
                    alert("success");
                },
                error: function(error){
                    alert("error");


                }
            });


        }); 

and my rest controller is this

@PostMapping(consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public String createMembers(Member member) {
    if (member.hasError()) {
        throw new CreatePersonException("All the fields must be filled properly in order to create a new member.");
    }

    if (personDao.createMember(member)) {
        return "Member: " + member.getFirstName() + " " + member.getLastName() + " successfully created.";
    }

    return "couldn't create member, please try again later.";

}

I have a create member button in the webpage that executes this ajax post method, when I press it, everything works fine, the member information gets sent to the rest controller, It gets created (100% working) and returns {"readyState":4 status":200, "statusText":"parsererror"} and error: function(error) of ajax post method gets called

What's the problem?

it's the first day I'm working with ajax and javascript, I don't really understand what's happening.

Thanks in advance

P.S I've tried changing data-type text json, json and some others that been suggested in similar questions, but they didn't work for me, so i decided to make a question myself.

Amirreza Yegane
  • 315
  • 1
  • 5
  • 14
  • What does "the member gets created" mean? What do you see in your browser's Network tab? Does the Spring application show anything on the console? (Also note: Throwing an exception is a good way to handle an error; returning a random string with a human-readable error message is not. You can use `@Valid` on your `Member` to have Spring do the check for you. In Java, methods should start with lowercase (`createMember`), and you should investigate Spring Data, which will autogenerate repositories. For a POST, you probably want to return a `ResponseEntity` with a `Location` header.) – chrylis -cautiouslyoptimistic- Sep 23 '19 at 04:10
  • @chrylis thanks for the comment. for the naming of CreateMethod, that was just a mistake I didn't notice, and "the member gets created" means everything works fine, as you can guess by the method's name the goal is to create a member. I'll try returning a ResponseEntity to see what will happen. – Amirreza Yegane Sep 23 '19 at 04:23
  • @chrylis I changed my rest controller to return ResponseEntity but still getting the same result. – Amirreza Yegane Sep 23 '19 at 05:07

2 Answers2

1

Try changing

data: JSON.stringify(member) and you will have your response in your success in the result/

0

Remove data type from ajax request and it works.

Remove this.

dataType: "application/json"

For details refer this

Alien
  • 15,141
  • 6
  • 37
  • 57
  • I tried `data: {//elements}` instead of `data: member` it worked, but I had to remove the `dataType: "application/json"` that you mentioned too to make it work. – Amirreza Yegane Sep 23 '19 at 05:38