javascript - angularjs http.post() 未收到响应

标签 javascript php html angularjs

    <!DOCTYPE html>
<html>
<head>
    <title>login</title>
    <script src="angular.min.js"></script>
    <style type="text/css">
        #a{
            display: none;
        }
    </style>
</head>
<body>


<div ng-app="myapp" ng-controller="myctrl">

<p>Input something in the input box:</p>
<input type="text" ng-model="username" placeholder="username"><br>
<input type="password" name="password" ng-model="password" placeholder="password"><br>
<button ng-click="login()">login</button>
<p id="a">
{{ resmsg }}<br>
<br></p>


</div>
<script type="text/javascript">
    var app= angular.module('myapp',[]);
    app.controller('myctrl', function ($scope , $http, $templateCache) {
        $scope.resmsg ="";

        $scope.login = function()
        {
            $http({
            method: 'POST', 
            url:'http://localhost/ng/myphp.php',
             data:{username : $scope.username, password : $scope.password},
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},  
            cache: $templateCache
        }).success(function(response) {
            $scope.resmsg = response;
        }).error(function(response){

        })
            $scope.resmsg = response;
        }


    })
</script>
</body>

</html>`


myphp.php

<? php

 $postdata = file_get_contents("php://input");
    $request = json_decode($postdata);
    $username = $request->username;
    $password = $request->password;
    if($username == "admin" && $password== "admin"){
        echo "1";
    }
    else {
        echo "0";
    }

?>

enter image description here

I was trying to create a simple login form using angularjs, php i have tried lot to send my form data to php file using http.post() method
in angular js but not getting any thing as a response and getting errormsg (screenshot) , the error is like there is no function like success help me to get rid from this error

最佳答案

您使用的 Angular 版本是什么? $http.success在 Angular 1.5 中已弃用,并在 Angular 1.6 中删除。您应该使用$http.then ,像这样:

$http({
    method: 'POST', 
    url:'http://localhost/ng/myphp.php',
    data:{username : $scope.username, password : $scope.password},
    cache: $templateCache
}).then(function(response) {
    $scope.resmsg = response.data;
}).catch(function(response){
    var err = response.data;
    // process error
})

接下来,在打开的 php 标签中出现错误,有一个额外的空格(使用 <?php 而不是 <? php

此外,您的服务器似乎没有使用 PHP 处理该文件,它只是返回 PHP 代码本身。尝试直接访问脚本并查看它是否返回代码或执行它。如果它以字符串形式返回代码,则表明您的 Web 服务器配置存在错误。

关于javascript - angularjs http.post() 未收到响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43573168/

相关文章:

javascript - 在 jQuery 中如何删除变量和函数

php - 在php中恢复数据库

javascript - 使用 PHP 在升序和降序日期之间切换 onclick

javascript - 给定代码的 Stripe 元素不显示自定义付款表单

javascript - 如何从 jquery ui datetimepicker 隐藏秒和毫秒

javascript - 点击事件覆盖自身 jQuery

php - 如何在 MySQL 的日期时间字段中存储 NULL 值?

c# - 更新后 css iframe 未在客户端上更新

html - 当用户单击其嵌套标签标记时,标记链接不起作用

javascript - 有没有办法只显示 extjs 树中的父节点