使用 Jquery 为 ajax 请求设置基本 url 的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6728545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:17:58  来源:igfitidea点击:

What is the best way to set up base url for ajax request using Jquery?

ajaxjquerycordova

提问by Alexey Zakharov

Since phonegap is based on local html file it doesn't know about base url of remote server. I wonder what is the best way to set base url for ajax requests using jquery? Could anybody provide link to sample?

由于 phonegap 基于本地 html 文件,因此它不知道远程服务器的基本 url。我想知道使用 jquery 为 ajax 请求设置基本 url 的最佳方法是什么?有人可以提供样品的链接吗?

Currently i think about using jquery ajax prefilter.

目前我正在考虑使用 jquery ajax prefilter。

回答by samy-delux

The semantically correct way of doing this is to use the <base>tag:

这样做的语义正确的方法是使用<base>标签:

<head>
    <base href="http://mydomain.com/">
</head>

Then retrieve the value like this (unfortunately jQuery.ajax() does not pick up this value automatically):

然后像这样检索值(不幸的是 jQuery.ajax() 不会自动获取这个值):

$('head base').attr('href');

Element reference: http://www.w3.org/TR/html4/struct/links.html#edef-BASE

元素参考:http: //www.w3.org/TR/html4/struct/links.html#edef-BASE

回答by user393274

Both of the established answers have major drawbacks. Requiring a function to be called to massage your URL every time you make a request is a lot of extra typing. Adding a base tag to a page works great most of the time, but it causes some trouble if you're doing heavy duty work with SVGs. Here's a simple solution that fixes all your URLs at once without using the base tag.

这两个既定答案都有主要缺点。每次发出请求时都需要调用一个函数来处理您的 URL,这是很多额外的输入。向页面添加基本标签在大多数情况下效果很好,但如果您正在使用 SVG 进行繁重的工作,则会带来一些麻烦。这是一个简单的解决方案,可以在不使用基本标记的情况下立即修复所有 URL。

var baseUrl = 'http://my.hardcoded.url/';
$.ajaxSetup({
    beforeSend: function(xhr, options) {
        options.url = baseUrl + options.url;
    }
})

回答by ghayes

When I dealt with this issue, I just put that information as a data-element on the tag.

当我处理这个问题时,我只是将该信息作为数据元素放在标签上。

 <body data-base="http://mydomain.com"> ...

And then used that in building out the proper URL for a given request:

然后在为给定请求构建正确的 URL 时使用它:

 //If pathOrURL is a relative path (e.g. /users/1), then we return a qualified
 // URL, such as http://mydomain.com/users/1
 // otherwise, we return the URL as is
 var qualifyURL = function(pathOrURL) {
   if (!(new RegExp('^(http(s)?[:]//)','i')).test(pathOrURL)) {
     return $(document.body).data('base') + pathOrURL;
   }

   return pathOrURL;
 };

 //Use this helper function when calling $.ajax
 $.ajax({
   url: qualifyURL(url), ... });

This worked great for my experience with Phonegap. Hopefully this helps.

这对我使用 Phonegap 的体验非常有用。希望这会有所帮助。

回答by Sam S

Remove leading slash to make ajax url relative to base defined in header.

删除前导斜杠以使 ajax url 相对于标头中定义的基数。

<head>
    <base href="http://base.com/">
</head>
$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        settings.url = settings.url.replace(/^\//,'')
    }
})