Relative Path Problems in Javascript Ajax call

后端 未结 5 752
深忆病人
深忆病人 2020-12-10 02:14

Okay, I have a JavaScript file with the following functions:

function AskReason() {
    var answer = prompt(\"Please enter a reason for this action:\", \"\")         


        
5条回答
  •  醉话见心
    2020-12-10 02:54

    I had a similar problem where an absolute URL was needed but the reference broke when going from localhost to the production server. I resolved it by checking if a "localhost" string exists in:

    var environ = window.location.host;
    

    Then you can simply do:

    if (environ === "localhost") {
        var baseurl = window.location.protocol + "//" + window.location.host + "/" + "shared/";
    } else {
        var baseurl = window.location.protocol + "//" + window.location.host + "/";
    }
    

    Then you can add baseurl in front of whatever url you need to reference.

提交回复
热议问题