简体   繁体   English

$ http在Angularjs 1.x中不起作用

[英]$http not working in Angularjs 1.x

I am stuck with angular's all type of $http requests I am trying to hit the rest API with $http but my code ran through it and do not show any call in chrome network tab. 我对angular的所有类型的$ http请求都感到困惑,我试图用$ http来访问其余的API,但是我的代码通过了它,并且在chrome网络标签中不显示任何调用。 I am using python simplehttpserver for my project execution. 我正在使用python simplehttpserver执行项目。

Below is the code of the service I am trying to hit 以下是我尝试访问的服务的代码

HTML CODE HTML代码

<form  class="form-horizontal" method="post" ng-submit="loginModel.login()">
            <div class="form-group">
                <div class="col-md-12">
                    <input type="text" class="form-control" placeholder="Username" ng-model="loginModel.username"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12">
                    <input type="password" class="form-control" placeholder="Password" ng-model="loginModel.password"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-6">
                    <a href="#" class="btn btn-link btn-block">Forgot your password?</a>
                </div>
                <div class="col-md-6">
                    <button type="submit" class="btn btn-info btn-block">Log In</button>
                </div>
            </div>
        </form>

Controller Code 控制器代码

module.exports = function ($scope,$rootScope,$state,crudFactory,$http,$resource) { module.exports =函数($ scope,$ rootScope,$ state,crudFactory,$ http,$ resource){

var vm = this;
vm.login = login;

function login() {
    var  userDetail = {
        username:vm.username,
        password:vm.password
    };


    $http({
        method: 'POST',
        url:'http:example.com',
        data:userDetail,
    }).then(function (response) {
       console.log(response)
    }, function (response) {

    });
}

I have inject $http in my controller and have also tried to hit the API with $resource but nothing is happening when i hit the service on click event. 我已经在控制器中注入了$ http,并且还尝试使用$ resource来访问API,但是当我在click事件上点击服务时什么也没有发生。

I have tried this with core javascript and it works fine but why its not working $http or $resouce 我已经尝试过使用核心javascript,它可以正常工作,但是为什么它不能正常运行$ http或$ resouce

var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://www.example.com/", false);
    xhr.send();
    console.log(xhr.status);

Please help me on this 请帮我

Thanks 谢谢

It looks like your URL is not formatted correctly, and you aren't doing anything in the event of an error. 看来您的URL格式不正确,并且在发生错误时您什么也不做。

 $http({
    method: 'POST',
    url:'http://example.com',
    data:userDetail,
}).then(function (response) {
   console.log(response)
}, function (response) {
   console.log('error response', response);
});

This will at least show something in the console if there is an error. 如果有错误,这至少会在控制台中显示一些内容。

If the url change still isn't working, check your browser developer console's network tab to see if the request is being made and if so, if it is timing out (stuck in pending state) 如果网址更改仍然无效,请检查浏览器开发者控制台的“网络”标签,以查看是否正在发出请求,如果正在发出请求,请求是否超时(处于待处理状态)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM