jQuery Relative URL Not Working
-
21-12-2019 - |
Question
I am having issues with jQuery. What is weird however is that it will work on on web container, but not another. My issue is that I am making an AJAX call to the back end, but it fails to construct the correct URI (it only does this on Oracle Weblogic, it has been tested and it works on Tomcat 7 and Wildfly).
So essentially my code looks like this:
$.get(
"api/data/drawing?drawing=1&user=anon",
{},
function(data) {
...
});
On Wildfly and Tomcat it will correctly call the URL:
http://192.168.1.100:8080/MyApp/api/data/drawing?drawing=1&user=anon
But on Weblogic it will call:
http://192.168.1.100:8080/api/data/drawing?drawing=1&user=anon
Although the app is indeed mapped to /MyApp (which is the artifact id of the war). I can tell because static resources like images and links will point to the correct uri.
I am lacking options right now because we are in a crunch. I would love to say just don't use Weblogic, or hard code the URL's, but that seems hacky to me. And more so, if the client ever relocates the webserver, we would be in trouble. Please help me figure this out =(. I imagine it's a Weblogic issue (or perhaps their server isn't correctly configured), and not a problem with my code. So I realize you might not be able to help me, but it's worth a shot right? Thanks in advance!
Solution
This sounds like you haven't set your context-root
for your application in Weblogic. You will want to set it in your application.xml or weblogic.xml:
<weblogic-web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app
http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<context-root>MyApp</context-root>
</weblogic-web-app>
You can see some good example here in the Oracle docs
OTHER TIPS
Could you just set a variable for a baseURL:
var baseUrl = "http://192.168.1.100:8080/MyApp/";
$.get(
baseUrl + "api/data/drawing?drawing=1&user=anon",
{},
function(data) {
...
});