I've been attempting to send a JSON file through AJAX post to my controller for API consumption, but I haven't received any response.
Below is the script containing the JSON object:
var jsonObjects = {
"vehicle": {
"id": "272",
"year": "2017",
"marketValue": {
"amount": 345000,
"currency": "MXN"
}
},
"downPayment": {
"amount": 34500,
"currency": "MXN"
},
"installmentPlanTerms": {
"number": "36",
"frequency": "MONTHLY"
},
"casualtyInsurance": true,
"lifeInsurance": false
};
Here is the AJAX request where the controller URL is included:
$.ajax({
type: 'post',
url: '/vehicle/cotizar',
data: JSON.stringify(jsonObjects),
contentType: "application/json; charset=utf-8",
traditional: true,
success: function (data) {
//document.log(data.data.requestedAmount.amount);
}
});
Below is my Java Spring controller code:
@RequestMapping(value="/vehicle/cotizar")
public String options(){
HttpHeaders headers = new HttpHeaders();
String token = "some key";
headers.set("Authorization","jwt ".concat(token));
headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
HttpEntity<String> entity = new HttpEntity<String>(headers);
return restTemplate.exchange("https://apis.bbvabancomer.com/loans_sbx/v1/options-installment", HttpMethod.POST, entity, String.class).getBody();
}
I'm hoping that the controller will return the JSON result I sent via AJAX. Refer to this image using Postman where I successfully sent a JSON file via POST to the API and received a response.