2017-07-02 98 views
0

嗨,我尝试后与AnguarJs数据,但有一些issues.My js代码无法发布低于:采用了棱角分明JS

var app = angular.module('myApp', []); 
    app.constant('appUrl', 'http://localhost/zara/rest/'); 
/*---- categories fetch code -------*/ 
    app.controller('myController',['$scope','$http','appUrl', function($scope, $http , appUrl) { 

     $scope.newName = ""; 
    $scope.postForm = function() { 
     var data = $.param({ 
       name: $scope.newName 
      }); 

     $http.post("http://localhost/zara/rest/api_customerlogin.php", data).success(function(data, status) { 
      $scope.hello = data; 
      console.log(data); 
     }); 
    }; 
    }]); 

我收到此错误TypeError: Cannot read property 'param' of undefined当我改变代码赞一个然后我得到这个错误。

<script> 
    angular.module('myApp', []) 
    .controller('myController', ['$scope', '$http', function($scope, $http) { 
     this.loginForm = function() { 

      var user_data='user_email=' +this.inputData.email+'&user_password='+this.inputData.password; 

      $http({ 
       method: 'POST', 
       url: 'login.php', 
       data: user_data, 
       headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
      }) 
      .success(function(data) { 
     console.log(data); 

      }) 
     } 

    }]); 
    </script> 

不能张贴到login.html的page.This是我在AnguarJs第一个项目这样不是很familer与this.Thanks。

+0

你有没有尝试在服务文件:app.serivce(“服务”,[$ HTTP,函数(){}]) – amrography

+0

不,我刚试过的代码与上述codes.How将服务工作与此? –

+0

什么是'$ .param'为你做什么?!,将你的数据更改为'object'作为'var data = {name:$ scope.newName}' – Maher

回答

0

在你的第一个代码,您尝试发布到

http://localhost/zara/rest/api_customerlogin.php 

你忘了你的第二个例子来改变login.php这个网址?

+0

在第二个我只是粘贴代码与虚拟URL.URL更改没有影响的代码。 –

0

请注意,直到您为浏览器添加allow-orgin-access插件后,发布请求才可能在本地环境中正常工作。但它适用于实际工作的网站。

var app = angular.module('myApp', []); 

app.constant('appUrl', 'http://localhost/zara/rest/'); 

app.controller('myController',function($scope, service, appUrl){ 

    $scope.newName = ""; 
    $scope.loading = false; 

    $scope.postForm = function() { 

    var data = $.param({ 
      name: $scope.newName 
     }); 

    $scope.loading = true; 
    service.makeThisRequest(appUrl + 'api_customerlogin.php', data).then(function(data){ 
     $scope.hello = data; 
     console.log(data); 
     },function(error){ 
     console.log(error); 
     }).finally(function(){ 
     $scope.loading = false; 
     }); 

    }); 

}]); 

app.factory('service', function ($http) { 
    return { 
     makeThisRequest : function(url,params){ 
     return $http.post(url,params); 
     } 
    }; 
}); 
+0

请解释'$ .param'吗?!为了什么? – Maher

+0

创建适合在URL查询字符串或Ajax请求中使用的数组,序列化对象或jQuery对象的序列化表示形式。在传递jQuery对象的情况下,它应该包含具有名称/值属性的输入元素。 http://api.jquery.com/jquery.param/ – amrography

+0

我已经在PHP端添加了允许源代码。 –