我启用了 CORS 模块并在我的 nodeJS 服务器代码中设置了所需的选项。我能够将 GET 请求从我的浏览器发送到给定的 URL 并获得响应。但是,当我尝试执行以下代码以使用来自 WebStorm IDE 内置服务器的以下 AngularJS 代码发送 GET 请求时,出现以下错误。
city name Delhi
error SyntaxError: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL
代码如下。
mainApp.controller('dController', function($scope,$http) {
this.ccaid = {
city:'',
circle:'',
area:'',
};
this.getCircle=function(){
console.log('city name ' + this.ccaid.city);
$http({
method: 'GET',
crossDomain:true,
url: 'xx.xxx.xx.xx:3000/fetchCityArea',
params:{city:this.ccaid.city}
}).then(function(success){
console.log('response ' + success);
},function(error){
console.log('error ' + error);
});
};
});
URL 没有问题,但仍然无法发送 GET 请求。
如何解决?
最佳答案
您的网址是否包含方案?您的示例代码没有:
url: 'xx.xxx.xx.xx:3000/fetchCityArea',
那需要有scheme部分——http://
或https://
:
url: 'https://xx.xxx.xx.xx:3000/fetchCityArea',
关于javascript - 使用 AngularJS 发送 GET 请求时无法从 WebStorm IDE 执行 'open' 无效 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42247444/