我已经在我的AngularJS应用程序中将$http > URL参数更改为
```javascript { withCredentials: true } ```这是因为我需要从不同的域中获取数据。但是,现在我收到了一个错误消息:“无法使用凭据执行XMLHttpRequest:跨源请求时,只能使用凭据提供文件名和密码”。
有什么方法可以解决这个问题吗?
$http({ method:'POST',
url:'http://localhost/api/clients.php'
}).success(function(data,status,headers,config){
deferred.resolve(data);
}).error(function(data,status,headers,config){
deferred.reject(status);
});
to
$http({ method:'POST',
url: ConfigService.Urls.Clients
}).success(function(data,status,headers,config){
deferred.resolve(data);
}).error(function(data,status,headers,config){
deferred.reject(status);
});
以下是 ConfigService 的代码:
Urls: {},
loadUrls: function(){
Urls.Clients = 'http://localhost/api/clients.php';
}
当然,在通过.Resolve加载控制器之前,我调用了loadUrls,因此我可以100%确定Urls.Clients不为null。
在我进行这个更改后,我开始收到错误:
TypeError: Cannot read property 'protocol' of undefined
at urlIsSameOrigin (http://localhost/myapp/app/lib/angular/angular.js:13710:17)
at $http (http://localhost/myapp/app/lib/angular/angular.js:7508:23)
令人困惑的是,除了我遇到的错误之外,该应用程序完全正常运行... 请问有人能告诉我在这里做错了什么,以及为什么会出现这个错误,如果该应用程序已经没有问题的话。
Urls.Clients
必须以某种方式为undefined
。很抱歉不断地问问题,但是loadUrls()
在哪里调用呢?我在你的代码中没有看到它。另外,如果您只是在服务中设置URLS而不是将其放入函数中,是否会更好? - drew_w