nikotromus
nikotromus

Reputation: 1064

Passing an integer from AJAX to Rest

I have an image I am sending to a rest service through an ajax call. I also need to send three integer values that identifies what form the image goes with. It all works, except when the identifiers are null. On the rest side, the null values are converted to a String. However, the data types I have set up on the java service is are Integers, so the service call bombs out.

I could just change the Rest input parameters in Java to Strings and then convert them to Integers on the Java side, but that seems like a hack.

formData.append('file', file);
formData.append('formId', this.props.builder.formId);
formData.append('formVersionId', this.props.builder.formVersionId);
formData.append('formIndex', this.props.builder.formIndex);

$.ajax({
    url: URL.BUILDER_URL + '/megaUpload',
    type: 'POST',
    data : formData,
    cache : false,
    contentType : false,
    processData : false,
});

Upvotes: 0

Views: 379

Answers (1)

ren.rocks
ren.rocks

Reputation: 792

You can prevent the calls before they are sent by using this conditional before your call:

typeof formId !== 'number'

or check if it is null and convert it to zero and avoid making the call as well there:

typeof formId === 'undefined' || !formId

if there is a form, you can require a number and default the value to zero. Note that this would only work on browsers that support html5:

<input type="number" min="0" name="formId" />

Upvotes: 1

Related Questions