1
votes

Using the following code:


    var form = new FormData();
    form.append("active_registration_id", "123");
    form.append("language_id", "79");
    form.append("archived", "{{archived}}");
    form.append("enabled", "{{enabled}}");
    form.append("password", "SamplePassword");
    form.append("password_confirmation", "SamplePassword");
    form.append("registration_attributes[first_name]", "Justin");
    form.append("registration_attributes[last_name]", "Trudeau");
    form.append("registration_attributes[email]", "[email protected]");
    form.append("registration_attributes[telephone_1]", "555-555-5555");
    form.append("registration_attributes[date_of_birth]", "1943-10-10");
    form.append("registration_attributes[gender]", "Male");
    form.append("registration_attributes[referral_code]", "Ample Clinic 123");
    form.append("registration_attributes[status]", "Registration Pending");


    var settings = {
      "async": true,
      "crossDomain": true,
      "url": "https://www.sample.com/v2/clients?token=DMfJjzWLngIn0JBHA0gWcg",
      "method": "POST",
      "headers": {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      "processData": false,
      "contentType": false,
      "mimeType": "multipart/form-data",
      "data": form
    }

    $.ajax(settings).done(function (response) {
      console.log(response);
    });

We always get 400 (Bad Request) with further error: {"registration_attributes":["Missing"]} This code will work in Postman, but not on the production server. Is there anything wrong with how I am creating the registration_attributes array?

1

1 Answers

1
votes

FormData sets the content type of the ajax request, remove the content type header you set.

P.s. mimeType is not a field in jQuery ajax, just remove that.