dtsps2098 2016-02-09 01:43
浏览 44
已采纳

如何使用http post AngularJS将数据从php返回到页面?

My AngularJs page

  var postContactFn = function($http,data){
        return $http.post("myfirstfile.php", data);
    }

mainModule.factory('postContact',['$http',postContactFn]);

//In my controller

    postContactFn(data).success(function(data, status) {
        if(data){
                  console.log('success');
          }
    }

Php page MyFirstFile.php

 <?php
$name = $_POST['inputUsername']; 
if(!empty($name)){
  --send mail
 echo 'success'
}

Here the mail is been sent to my id but I'm not getting redirected back the controller. Instead it is showing success on my page.

Any suggestions would be appreciated.. Thank you

  • 写回答

1条回答 默认 最新

  • duanji1899 2016-02-09 01:53
    关注

    You pass the data as dependency, not to a function.

    This is how it should be:

    mainModule.controller('MyController', function($scope, postContact) {
      postContact.postData(data).then(function(data)) {
        console.log(data);
      });
    });
    

    And your factory:

    mainModule.factory('postContact', function($http) {
      return {
        postData : function(data) {
          return $http.post("myfirstfile.php", data);
        }
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,