Robin Sun
Robin Sun

Reputation: 1618

Relative path in Ajax works differently in different environment

It is a SpringBoot website. The html page url is http://xxxxx/trex/index/ And javascript code segment in index page as below

$(function(){

jQuery.ajax({
    contentType:'application/json;charset=UTF-8',
    type: "POST",
    url: "getSignTypes",
    cache: false,
    dataType: "json",
    success:function(data){
            if(data !== 'NA'){
                console.log(data);
                $('#signType').combobox({
                    valueField:'id',
                    textField:'title',
                    editable:false,
                    data:data,
                    value:data[0].id
                });
            }
    },
    error:function(msg){
        console.log(msg)
    }
});
})

You can see I use relative path in url parameter of this ajax request. I guess since it is relative url, it should be converted into http://xxxxx/trex/index/getSignTypes. I test it in my local, and yes, it is as expected http://localhost:8088/trex/index/getSignTypes.

But when I deploy it to UAT, I find that the url is converted to http://hswcfc-trainexp-web.uat.homecreditcfc.cn/trex/getSignTypes. The index part is gone.

Why relative path in Ajax works differently in different environment? The ajax code is exactly the same. Any clue I can trace to find the difference? Thanks.

I past the UAT screen shot here. enter image description here

Upvotes: 0

Views: 138

Answers (1)

Amadan
Amadan

Reputation: 198314

A HTTP URL consists of several parts: protocol, hostname, port, username, password, path, query string (?....) and fragment (#....).

As suspected, your URL path ends in / in dev, but not in UAT. Think of it as "directories": /trex/index/ is the empty file name in /trex/index directory, while /trex/index is the file index in /trex directory. Web servers often treat the two the same way, but clients do not: when you do a relative path from there, you get /trex/index/getSignTypes in the first case, but /trex/getSignTypes in the second.

This is usually fixed by creating a redirect rule so that you can never accidentally write the same URL in two different ways (e.g. by redirect /trex/index to /trex/index/).

Upvotes: 1

Related Questions