相关疑难解决方法(0)

如何将数据作为表单数据而不是请求有效负载发布?

在下面的代码中,AngularJS $http方法调用URL,并将xsrf对象作为"请求有效负载"提交(如Chrome调试器网络选项卡中所述).jQuery $.ajax方法执行相同的调用,但将xsrf提交为"Form Data".

如何让AngularJS将xsrf作为表单数据而不是请求有效负载提交?

var url = 'http://somewhere.com/';
var xsrf = {fkey: 'xsrf key'};

$http({
    method: 'POST',
    url: url,
    data: xsrf
}).success(function () {});

$.ajax({
    type: 'POST',
    url: url,
    data: xsrf,
    dataType: 'json',
    success: function() {}
});
Run Code Online (Sandbox Code Playgroud)

ajax post angularjs angular-http

517
推荐指数
12
解决办法
39万
查看次数

$ resource transformResponse无效

$resource这里有一个简化的例子(改编自Angular网站):

angular.module('project', ['mongolab']);

function ListCtrl($scope, Project) {
  $scope.projects = Project.test();
}

angular.module('mongolab', ['ngResource']).
factory('Project', function ($resource) {
  var url, dfParams, actions;

  url = 'https://api.mongolab.com/api/1/databases' + '/angularjs/collections/projects/:id';
  dfParams = {
    apiKey: '4f847ad3e4b08a2eed5f3b54'
  };

  actions = {
    test: {
      method: 'GET',
      isArray: true,
      transformResponse: function (response) {
        // line is never getting called
        console.log('transforming');
        return response;
      }
  };

  var Project = $resource(url, dfParams, actions);
  return Project;
});
Run Code Online (Sandbox Code Playgroud)

问题是线路console.log('transforming')永远不会被调用.这是为什么?其他一切都很好.

在这里小提琴.

javascript angularjs

5
推荐指数
1
解决办法
4123
查看次数

标签 统计

angularjs ×2

ajax ×1

angular-http ×1

javascript ×1

post ×1