0

I have to send data from view to controller using ajax. Here is my class and I should send JSON appropriate to that class structure. Jackson will convert JSON to my class

public class RealEstateAgencyDTO extends BaseEntityDTO {

/** The name. */
private String name;

/** The description. */
private String description;

/** The site. */
private String site;

/** The phone number. */
private String phone;

/** The address of the office. */
private AddressDTO address;


public final String getName() {
    return name;
}

public final void setName(final String newName) {
    this.name = newName;
}

public final String getDescription() {
    return description;
}

public final void setDescription(final String newDescription) {
    this.description = newDescription;
}

public final String getSite() {
    return site;
}

public final void setSite(final String newSite) {
    this.site = newSite;
}

public final String getPhone() {
    return phone;
}

public final void setPhone(final String newPhone) {
    this.phone = newPhone;
}

public final AddressDTO getAddress() {
    return address;
}

public final void setAddress(final AddressDTO newAddress) {
    this.address = newAddress;
}

}

how should I use JSON.stringify() in order to get such an object that correspond my structure

I've tried to use smth like that but it doesn't work

var address = JSON.stringify({
                country: $('#country').val(),
                region: $('#description').val(),
                postalCode: $('#postalCode').val(),
                locality: $('#locality').val(),
                additionalInfo: $('#additionalInfo').val()
            });

            var data = {
                agencyName: $('#agencyName').val(),
                description: $('#description').val(),
                phoneNumber: $('#phoneNumber').val(),
                webSite: $('#webSite').val(),
                address: address
            };

             $.ajax({
                   type: "post",
                   url: "registerAgency",
                   data:  JSON.stringify(data),
                   contentType: "application/json",
                   success: function(responseData, textStatus, jqXHR) {
                       alert("data saved")
                   },
                   error: function(jqXHR, textStatus, errorThrown) {
                       console.log(errorThrown);
                   }
               })
0

2 Answers 2

1

You're over-complicating it. Don't stringify until the very end, otherwise you will end up with json inside of json, which is unlikely to be useful in any situation.

var address = {
    country: $('#country').val(),
    region: $('#description').val(),
    postalCode: $('#postalCode').val(),
    locality: $('#locality').val(),
    additionalInfo: $('#additionalInfo').val()
};

var data = {
    agencyName: $('#agencyName').val(),
    description: $('#description').val(),
    phoneNumber: $('#phoneNumber').val(),
    webSite: $('#webSite').val(),
    address: address
};

$.ajax({
    type: "post",
    url: "registerAgency",
    data:  JSON.stringify(data),
    contentType: "application/json",
    success: function(responseData, textStatus, jqXHR) {
        alert("data saved")
    },
    error: function(jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

the address member on object data is already stringified. The subsequent call will treat this as a string value (which it is!) JSON.stringify() will handle nested objects fine.

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.