无法执行'打开'使用AngularJS发送GET请求时来自WebStorm IDE的无效URL

时间:2017-02-15 10:59:29

标签: angularjs node.js http cors webstorm

我启用了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);
        });
    };
});

网址没有任何问题,但仍然无法发送GET请求。

如何解决?

3 个答案:

答案 0 :(得分:4)

您的网址是否包含计划?您的示例代码不会:

url: 'xx.xxx.xx.xx:3000/fetchCityArea',

需要有一个方案部分 - http://https://

url: 'https://xx.xxx.xx.xx:3000/fetchCityArea',

答案 1 :(得分:0)

除了sideshowbarker的回答 -

网址应为WHATWG网址标准。 有效网址格式的示例如下所示 - https://url.spec.whatwg.org/#example-url-parsing

答案 2 :(得分:0)

我遇到的问题是我的URL无效。

http://ip::8080/

还有一个冒号。 我可能不是唯一的一个。