加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 为什么我的函数不发送POST

发布时间:2020-12-17 17:17:38 所属栏目:安全 来源:网络整理
导读:这是参考破损代码的 fiddle.当我尝试它时,它应该发送到的服务器没有响应,这使我相信它没有收到来自我的应用程序的任何流量.问题显而易见吗?如果没有,我可以使用哪些故障排除方法来调查问题? stackoverflow希望我将我的代码包含在内,所以在这里. 的index.ht
这是参考破损代码的 fiddle.当我尝试它时,它应该发送到的服务器没有响应,这使我相信它没有收到来自我的应用程序的任何流量.问题显而易见吗?如果没有,我可以使用哪些故障排除方法来调查问题?

stackoverflow希望我将我的代码包含在内,所以在这里.

的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',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});
       };



});

解决方法

目前,每次用户单击按钮时,您都会创建相同的资源.

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

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

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

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

方法$save继承自ngResource并执行POST请求.这是一个jsfiddle http://jsfiddle.net/jaimem/FN8Yg/19/

在开发人员工具中,请求方法将被列为OPTIONS,因为它是从jsfiddle创建的.请求参数将是这些方面的东西

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

你可以阅读更多$resource here

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读