使用web API并得到一个引用错误

consuming a web api and getting a referenceerror

本文关键字:一个 引用 错误 API web 使用      更新时间:2023-09-26

当试图从我的web api发布和获取数据时,我在浏览器中得到这个异常:

angular.js:13708 ReferenceError: $没有定义(GbyG.js:23)在对象。调用(angular.js: 4709)$controllerInit (angular.js:10234)(angular.js:9147)在复合链接fn (angular.js:8510)(angular.js:9210)在复合链接fn (angular.js:8510)在复合链接fn (angular.js:8513)在复合链接fn (angular.js:8513)at publicLinkFn (angular.js:8390)

这是我的客户端(用angular写的):

app.controller('GetSamplesByStatus',
function($scope, $http) {
    var serializedData = $.param({statustype:"Received"});
    $http({
        method: 'POST',
        url: ("http://localhost:36059/api/Samples/GetSamplesByStatus"),
    data: serializedData,
    headers: {
        'Content-Type': 'application/json'
    }}).then(function(response) {
        //$scope.dataset = response;
        console.log(error);
    }, function(error) {
        console.log(error);
    });
});

我做错了什么?

参见angular文档中的$httpParamSerializerJQLike它的序列化工作类似于jQuery.param() link

.controller(function($http, $httpParamSerializerJQLike) {
  //...
  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
});

就像这样:

app.controller('GetSamplesByStatus',
    function($scope, $http, $httpParamSerializerJQLike) {
        var myParams = {statustype:"Received"};
        $http({
          url: "http://localhost:36059/api/Samples/GetSamplesByStatus%0A",
          method: 'GET',
          params: myParams,
          paramSerializer: '$httpParamSerializerJQLike'
        }).then(function(response) {
            //$scope.dataset = response;
            console.log(response.data);
        }, function(error) {
            console.log(error);
        });
    });

查看这里的工作示例:http://jsbin.com/madetefeya/edit?js,console