2016-04-08 86 views
0

这是我的index.html页:角发帖不发送

<!DOCTYPE html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>My AngularJS App</title> 
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css"> 
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css"> 
    <link rel="stylesheet" href="app.css"> 
    <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 
    <script src="app.js"></script> 
</head> 
<body> 
    <div ng-controller="HomeCtrl"> 
     <form ng-submit="sendPost()"> 
     <label>Input id</label> 
     <input type="text" ng-model="id" required/> 
     <button type="submit">Submit</button> 
     </form> 
     {{response}} 
    </div> 
</body> 
</html> 

这里是我的app.js

'use strict'; 

// Declare app level module which depends on views, and components 
angular.module('myApp', []) 
.controller('HomeCtrl', function($scope, $http) { 
    $scope.id = ""; 
    $scope.response = ""; 
    $scope.sendPost = function() { 
     var userDetails = [{"userId":"23848348", "type":"CUSTOMER"}]; 
     var request = { 
      method: 'POST', 
      url: 'http://thisisatest.testhost.com/userInfo', 
      headers: { 
       'Content-Type': 'application/json', 
       'id':$scope.id 
      }, 
      data: $.param({ 
       json: JSON.stringify({ 
        'code':'8388', 
        'userDetails': userDetails 
       }) 
      }) 
     } 
     $http(request) 
      .success(function(data, status, headers, config){ 
       /*called for result & error because 200 status*/ 
       if (data.result) { 
        $scope.response = data.result; 
       } else if (data.error) { 
        //handle error here 
       } 
      }) 
      .error(function(data, status, headers, config){ 
       /*handle non 200 statuses*/ 
      }); 
    } 
}) 

这里是JSON的请求体应该看起来像:

{ 
    "code":"8388", 
    "userDetails": [ 
     {   
      "userId": "23848348",   
      "type": "CUSTOMER" 
     } 
    ] 
} 

但是,当我输入一个ID在文本框和点击提交我没有看到任何东西打印在响应区域。任何想法我在这里做错了吗?

+0

您在HTML – swapnesh

+0

@swapnesh错过'NG-应用=“对myApp”'不幸的是没有解决的问题。 – Richard

回答

0

我发现了这个问题。请求的数据部分应该仅仅是:

data: { 
    'code':'8388', 
    'userDetails': userDetails 
}