Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 设置全局基URL_Javascript_Jquery_Ajax_Global Variables - Fatal编程技术网

Javascript 设置全局基URL

Javascript 设置全局基URL,javascript,jquery,ajax,global-variables,Javascript,Jquery,Ajax,Global Variables,我正在使用jQuery:哪种方法是存储AJAX请求的基本url值的最佳方法,每个脚本都可以以某种方式引用它,而无需对每次更新进行手动更改?我应该把它存储在窗口对象、html、全局变量还是什么?非常感谢。您可以创建函数自动执行此操作: var BASE_URL = 'http://www.foo.com/url-root/'; function getUrl(url) { return BASE_URL.concat(url); } 现在,当你想要一个url var url = get

我正在使用jQuery:哪种方法是存储AJAX请求的基本url值的最佳方法,每个脚本都可以以某种方式引用它,而无需对每次更新进行手动更改?我应该把它存储在窗口对象、html、全局变量还是什么?非常感谢。

您可以创建函数自动执行此操作:

var BASE_URL = 'http://www.foo.com/url-root/';

function getUrl(url) {
    return BASE_URL.concat(url);
}
现在,当你想要一个url

var url = getUrl('bar.php');  //returns 'http://www.foo.com/url-root/bar.php'
我想你不希望它是全球性的

(function() {
    var BASE_URL = 'http://www.foo.com/url-root/';

    window.getUrl = function(url) {
        return BASE_URL.concat(url);
    }
})();

或者您可以创建一个对象。

这将被一些人认为是危险的或不好的做法(请参阅下面的警告),但它将允许您保持ajax调用干净(没有显式的URL连接或方法调用)

这是通过覆盖
$.ajax
方法的默认行为来实现的,在将调用传递到真正的
$.ajax
方法之前,在URL上添加我们自己的预处理

这种方法与使用相同,因此我将从它们的文档中向您提供相同的警告:

此处指定的设置将影响对$.ajax或$.ajax的所有调用 基于Ajax的衍生工具,如$.get()。这可能导致不良后果 由于其他调用方(例如插件)可能期望 正常默认设置。因此,我们强烈建议 反对使用此API。相反,请在中显式设置选项 调用或定义一个简单的插件来执行此操作


或者你可以只做
BASE\u URL+'bar.php'
?好吧,我就这么说……我从哪里读到过globals是邪恶的?=)我会把它存储在一个全局变量中。哇,谢谢你的时间。我仔细阅读了它,它似乎是一个更健壮的解决方案。现在我必须决定。。。
function setBaseForAjax(base){
    var oa = $.ajax; //keep a reference to the actual ajax call
    $.ajax = function(){
        var len = arguments.length,
            newArgs = [],
            newUrl = len === 2 && (typeof arguments[0]).toLowerCase() === 'string' ? arguments[0]: null,
            newObj = len === 2 && (typeof arguments[1]).toLowerCase() === 'object' ? arguments[1] : (len === 1 && (typeof arguments[0]).toLowerCase() === 'object' ? arguments[0] : null); 

        if(newUrl){
            newUrl = base + newUrl; 
            newArgs.push(newUrl);  
        }
        if(newObj){
            newObj.url = base + newObj.url; 
            newArgs.push(newObj);    
        }
        oa.apply(window, newArgs); //call the real $.ajax method with the modified params
    }; 
}

setBaseForAjax('/echo/'); //set the base for every ajax call in the application.

$.ajax({
    url:'json/', 
    success: function(){
        alert('in for json');    
    }
}); 

$.ajax(
    'html/'
    ,{
        success: function(){
            alert('in for html');    
        }
    }
); 

$.ajax('html/', { //html wins over settings. 
    url:'json/', 
    success: function(){
        alert('in for html');    
    }
}); 

$.get('json/', function(){
    alert('in for json');
});