dongmi1872
dongmi1872
2015-07-17 08:12
浏览 38

Angular无法将函数的计算数据作为JSON发送

In a form, I am trying to send data as json to a php file, which is calculated correctly from a function in controller. But, if I try to send this calculated data as json, header shows as blank object. Can someone tell me why?

FORM:

<form id="form1" post="">
    <p ng-model="result">{{add()}}</p>
    <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          
</form>

CONTROLLER:

$scope.submitting = function(){
    var request = $http({ method: "post", url: "php/store.php", 
    data: {"Answer": $scope.result} });
    request.success(function (data) { 
        //alert("Successfully data entered! "); 
    });
}


$scope.add = function(){ 
    var a = 10; var b = 20; var c = a + b;
    return c;
}

图片转代码服务由CSDN问答提供 功能建议

在表单中,我试图将数据作为json发送到php文件,该文件是从函数中正确计算的 在控制器中。 但是,如果我尝试将此计算数据作为json发送,则标题显示为空白对象。 有人可以告诉我为什么吗?

FORM:

 &lt; form id =“form1”post =“”&gt; 
&lt; p ng-model =  “结果”&gt; {{add()}}&lt; / p&gt; 
&lt; button type =“submit”class =“btn btn-default”ng-click =“submitting()”&gt;提交&lt; / button&gt  ;  
&lt; / form&gt; 
   
 
 

CONTROLLER:

  $ scope.submitting = function(){
  var request = $ http({method:“post”,url:“php / store.php”,
 data:{“Answer”:$ scope.result}}); 
 request.success(function(data)  {
 // alert(“成功输入数据!”); 
}); 
} 
 
 
 $ scope.add = function(){
 var a = 10;  var b = 20;  var c = a + b; 
返回c; 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupi7619
    doupi7619 2015-07-17 08:19
    已采纳

    problem was inside your controller it should be like this

    FORM:

    <form id="form1" post="">
        <p >{{add()}}</p>
        <button type="submit" class="btn btn-default" ng-click="submitting()" >Submit</button>          
    </form>
    

    CONTROLLER:

    $scope.submitting = function(){
        var request = $http({ method: "post", url: "php/store.php", 
        data: {"Answer": $scope.add()} });
        request.success(function (data) { 
            //alert("Successfully data entered! "); 
        });
    }
    
    
    $scope.add = function(){ 
        var a = 10; var b = 20; var c = a + b;
        return c;
    }
    

    Fiddle:https://jsfiddle.net/xrx5Lxwk/1/

    点赞 评论

相关推荐