I have a (Spring-powered) Java application that has a some AJAX calls. The problem is that I'm using the application context (/spring-mvc) to be able to reach the server-side functionality.
var api = '/api/v1';
var context = '/spring-mvc' + api;
$.ajax({
type: 'GET',
url: context + '/users/' + $('#user-id').val()
}).done(function (response) {
callback({ data: response.user, binding: response.binding });
}).fail(function (jqXHR, textStatus, errorThrown) {
callback({ jqXHR: jqXHR });
});
Now, my problem is whenever I have to deploy it somewhere else, and I can't control the application context, the AJAX calls are eventually failing since the application context itself is hard-coded in the JavaScript.
Is there any way to achieve this without having to change the context variable in all JavaScript files? Something like:
$.ajax({
type: 'GET',
url: '/api/v1/users/' + $('#user-id').val()
}).done(function (response) {
callback({ data: response.user, binding: response.binding });
}).fail(function (jqXHR, textStatus, errorThrown) {
callback({ jqXHR: jqXHR });
});
I've seen that several times in many applications, but I can't figure it out how they do that.
Note: The Web application is deployed in the same WAR file; I would like to avoid to try to figure it out by doing URL manipulation in JavaScript.
/api/v1/is usually the equivalent to the context-path in Spring, whats your question=urlin the AJAX calls, it also fails. I want a way to get the application context dynamically (or some sort of)