且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不是我的函数发送POST

更新时间:2023-01-23 17:24:13

目前您是在每天用户点击按钮时创建相同的资源。

你可以做的是在您的控制器的地方创建服务

  VAR RES = $资源('http://10.64.16.6:3000/res/:id',{ID:'@id'});

然后,当用户点击按钮,创建资源的新实例,并将其传递数据要发送

  $ scope.sendJSON =功能(){
    $ scope.entry = angular.toJson($ scope.entryFields);
    变种R =新RES();
    R $保存({params:一个$ scope.entry});
};

该方法 $保存 ngResource 继承并做了 POST 请求。这里有一个的jsfiddle http://jsfiddle.net/jaimem/FN8Yg/19/

在开发工具的请求方法将被列为选项,因为它使得它的jsfiddle。该请求参数将沿着这些路线的东西。

  PARAMS:{ID:{pHolder:ID放在这里,fieldData:自卫队},说明:{pHolder:说明到这里,fieldData:自卫队},日期:{pHolder:去死日期放在这里,fieldData:自卫队}}

您可以了解更多$资源 rel=\"nofollow\">

Here's the fiddle in reference to the broken code. When I try it, the server it is supposed to be sending to doesn't respond, which leads me to believe it's not recieving any traffic from my app. Is the problem obvious? If not, what troubleshooting methods can I use to investigate the problem?

*** wants me to include my code inline so here it is.

index.html

<div ng-controller="DataEntryCtrl">
      <form ng-repeat="entryField in entryFields">
            <input type="text"
                   ng-model="entryField.fieldData"  
                   placeholder="{{entryField.pHolder}}">
      </form>

      <input type="button" ng-click="sendJSON()" value="Object To JSON" />
      <hr/>
       {{res}}
     <ul>
       <li>ID: {{entryFields.id.fieldData}}</li>
       <li>description: {{entryFields.description.fieldData}}</li>
       <li>date: {{entryFields.date.fieldData}}</li>
    </ul>            

</div>

controller.js

'use strict';
/* Controllers */
var app = angular.module('Hubbub-FrontEnd', ['ngResource']);

app.controller('DataEntryCtrl', function($scope,$resource) {
   $scope.entryFields = {
     id:           {pHolder:'ID goes here',fieldData:""},
     description:  {pHolder:'Description goes here',fieldData:""},
     date:         {pHolder:'Drop Dead Date goes here',fieldData:""}
   };

   $scope.showJSON = function() {
       $scope.json = angular.toJson($scope.entryFields);
   };
   $scope.sendJSON = function() {
       $scope.entry = angular.toJson($scope.entryFields);
       $scope.res = $resource('http://10.64.16.6:3000/Create',
         {create:{method:'POST'}},{params:$scope.entry});
       };



});

Currently you are creating the same resource every time the user clicks the button.

What you could do is create the service somewhere in your controller

var Res = $resource('http://10.64.16.6:3000/res/:id', {id: '@id'});

Then, when a user clicks the button, create a new instance of the resource and pass it the data you want to send

$scope.sendJSON = function() {
    $scope.entry = angular.toJson($scope.entryFields);
    var r = new Res();
    r.$save({params: $scope.entry});    
};

The method $save in inherited from ngResource and does a POST request. Here's a jsfiddle http://jsfiddle.net/jaimem/FN8Yg/19/

In the developer tools the request method will be listed as OPTIONS because it makes it from jsfiddle. The request params will be something along these lines

params:{"id":{"pHolder":"ID goes here","fieldData":"sdf"},"description":{"pHolder":"Description goes here","fieldData":"sdf"},"date":{"pHolder":"Drop Dead Date goes here","fieldData":"sdf"}}

You can read more $resource here