使用 AngularJS 发送 GET 请求时无法从 WebStorm IDE 执行 'open' 无效 URL
Failed to execute 'open' invalid URL from WebStorm IDE while sending GET request using AngularJS
我启用了 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 是否包含方案?您的示例代码没有:
url: 'xx.xxx.xx.xx:3000/fetchCityArea',
那需要有scheme部分—http://
或https://
:
url: 'https://xx.xxx.xx.xx:3000/fetchCityArea',
除了 sideshowbarker 的回答 -
URLs 应该是 WHATWG URL 标准。
有效 URLs 格式的例子在这里 - https://url.spec.whatwg.org/#example-url-parsing
我遇到的问题是我的URL无效。
http://ip::8080/
多了一个冒号。
我可能不是唯一一个。
我启用了 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 是否包含方案?您的示例代码没有:
url: 'xx.xxx.xx.xx:3000/fetchCityArea',
那需要有scheme部分—http://
或https://
:
url: 'https://xx.xxx.xx.xx:3000/fetchCityArea',
除了 sideshowbarker 的回答 -
URLs 应该是 WHATWG URL 标准。 有效 URLs 格式的例子在这里 - https://url.spec.whatwg.org/#example-url-parsing
我遇到的问题是我的URL无效。
http://ip::8080/
多了一个冒号。 我可能不是唯一一个。