0

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.

2
  • /api/v1/ is usually the equivalent to the context-path in Spring, whats your question= Commented Sep 10, 2015 at 15:12
  • Yes, but if I don't prepend the application context to the url in the AJAX calls, it also fails. I want a way to get the application context dynamically (or some sort of) Commented Sep 10, 2015 at 15:34

1 Answer 1

1

Deadly simple, you may include the context with a hidden input, such as

<input type="hidden" id="ctx" name="ctx" value="<applciation_context_here>"/>

then in your js

var context = document.getElementById("ctx").value/*getAttribute('value')*/ + api;


or better way acquire the current windows location(path), and some string manipulation to get the context
update0:

FACTS:

Let say you will have /a , /b and /c contexts, I don't know but usually they will be mapped to a.com , b.com and c.com and context will be identified by the server as requested host, such as following

a.com -> local_host/a
b.com -> local_host/b

But if you are not going as above, as you are generating the jsp file, you may set the context path as hidden input approach

Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying not to do URL manipulation here, although it's also valid; the approach with the hidden field is the same like having a variable context in the JavaScript files
wonder what is the problem with url manipulation! the more logical way would be domain mapping with the context(check the update)
OK, maybe I forgot this: we are not using any server-side technology, just plain HTML, otherwise getting the application context would be trivial. When I say "application context", is not the domain, but the piece of the URL your application is running on; usually what comes after the <domain>:<port>, e.g. http://the-world:9080/application-context/api/v1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.