如果在我的cordova设置文件(config.xml)中,我使用此指令:
<access origin="http://domain.com" subdomains="true" />
(编辑 - 正如评论中所建议的那样)或尝试此指令:
<access origin="domain.com" subdomains="true" />
我的ajax请求失败(我将稍后提交目标URL和代码)。但如果我的设置是:
<access origin="*" />
然后我的ajax工作。这是执行ajax的代码和基本目标URL:
var baseUrl = "http://my.domain.com/index.php/base/path";
//... code omitted being irrelevant
function cancelCurrent($s) {
if ($s.canceller) {
$s.canceller.resolve();
delete $s.canceller;
}
}
function ajax($http, $s, $q, target, params) {
cancelCurrent($s);
$s.canceller = $q.defer();
return $http.get(target, {timeout: $s.canceller.promise, params: params || {}});
}
//... code omitted being irrelevant
//... now, this is a sample of how and where I use it:
var Application_ChooseSearchCategory = index.controller('Application.ChooseSearchCategory', ['$scope', '$rootScope', '$routeParams', '$location', '$http', '$q', function($s, $rs, $rp, $l, $http, $q){
var categoriesService = {
loadCategories: function(tipoBusqueda){
var sources = {"marca": "/brands", "tipo-vehiculo": "/vtypes"};
var source = sources[tipoBusqueda];
if (source) {
$s.buttons = [
{id: "getting-categories", text: "Obteniendo categorías...", section: true, click: function(){}}
];
var context = this;
ajax($http, $s, $q, baseUrl + source, {})
.success(function(data, httpStatus, headers, config, statusText){
$s.buttons = context.makeOptions(data.result, tipoBusqueda);
})
.error(function(data, httpStatus, headers, config, statusText){
$s.buttons = context.makeErrorOptions(tipoBusqueda);
});
} else {
$s.buttons = [
{id: "invalid-list-type", text: "Tipo de búsqueda inválido", section: true, click: function(){}}
];
}
},
makeOptions: [...] /* code omitted being irrelevant */,
makeErrorOptions: [...] /* code omitted being irrelevant */
};
categoriesService.loadCategories($rp.tipoBusqueda);
/* code omitted being irrelevant */
}]);
说明:根据$routeParams.tipoBusqueda
,针对“http://my.domain.com/index.php/base/path/brands”或“http://my.domain.com/index.php/base/path/vtypes”发出请求,并返回预期结果。
这样的网址存在,如果我使用通配符访问,代码可以完美地运行。如果我使用-instead-域访问,即使我指定subdomains =“true”,也会失败。
事实是:我想允许请求飞到domain.com
,但我不想拥有通配符访问权限 - 看起来不安全。
问题是:我该怎么做?(Cordova的3.4.0版本)。