【问题标题】:How to $http Get call with identifer如何 $http 获取带有标识符的呼叫
【发布时间】:2014-09-21 05:54:20
【问题描述】:

我需要使用 id 作为标识符进行 get 调用。我只使用 Angular 资源完成了此操作,而不是 $http。我不了解 $http 如何与 Angular 一起工作。

角度控制器

  $scope.EmailPdf = function () {
    $http.get('/api/Pdf/{id}').success(function () {
        $scope.PrintPreviewModal();
    });
}

路由

config.Routes.MapHttpRoute(
            name:"PdfApi",
            routeTemplate: "api/{controller}/{id}",
        defaults: new {controller = "apiGeneratePdf", id=RouteParameter.Optional}
        );

api 控制器

      public string Get(int id)
        {
        JobDataAdapter adapter = new JobDataAdapter();
        Job job = new Job();
        job = adapter.GetJob(id);
        if (job == null)
        {
            return string.Empty;
        }

        try
        {

错误信息

{"$id":"1","Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'TexasExterior.Controllers.PdfController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}

更新 网页配置

 config.Routes.MapHttpRoute(
        name: "PdfApi",
        routeTemplate: "api/pdf/{id}",
    defaults: new { controller = "PdfController", id = RouteParameter.Optional }
    );

控制器

$scope.EmailPdf = function () {
    $http.get('/api/Pdf/', { id: id }).success(function () {
        $scope.PrintPreviewModal();
    });
}

错误信息

ReferenceError: id 未定义

控制器

 var id=1    
$scope.EmailPdf = function () {
    $http.get('/api/Pdf?id=id').success(function () {
        $scope.PrintPreviewModal();
});

}

错误信息

{"$id":"1","Message":"请求无效。","MessageDetail":"参数字典包含不可为空类型'System.'的参数'id'的空条目。 'TexasExterior.Controllers.PdfController' 中方法 'System.String Get(Int32)' 的 Int32'。可选参数必须是引用类型、可空类型或声明为可选参数。"}

控制器

var _id = 1;
var URL = '/api/Pdf';
$scope.EmailPdf = function () {
    $http.get(URL, { id: _id }).success(function () {
        $scope.PrintPreviewModal();
    });
}

错误信息

{"$id":"1","Message":"No HTTP resource was found that matches the request URI 'http://localhost:44301/api/Pdf'.","MessageDetail":"No action was found on the controller 'Pdf' that matches the request."}

【问题讨论】:

  • 您不会在 javascript 代码中使用模板语法 {id}。大概 id 在脚本本地可用,在这种情况下,您只需要类似 "$http.get('/api/Pdf/'+id).success(function () {"
  • 错误信息非常具有描述性,你没有为 id 传递任何东西,试试这个:$http.get('/api/Pdf/'+id)
  • HTTP 错误 404.11 - 未找到 请求过滤模块被配置为拒绝包含双转义序列的请求。
  • 看起来像一个安全问题?

标签: ajax asp.net-mvc angularjs asp.net-web-api


【解决方案1】:

怎么样

  $http.get(URL,{id:id})

即作为地图或字符串的 get 调用的第二个参数传入您的参数。

【讨论】:

  • $http.get('/api/Pdf/', { id: id })
  • ReferenceError: id is not defined
【解决方案2】:

您的id 未定义

PdfController

将您的配置路由更新为:

config.Routes.MapHttpRoute(
            name:"PdfApi",
            routeTemplate: "api/pdf/{id}",
        defaults: new {controller = "PdfController", id=RouteParameter.Optional}
        );

否则你可以使用不同的语法,即:

 var id=1    
 $scope.EmailPdf = function () {
    $http.get('/api/Pdf?id=id).success(function () {
        $scope.PrintPreviewModal();
    });
}

 var _id=1;   
 var URL='/api/Pdf' ;
 $scope.EmailPdf = function () {
   $http.get(URL,{id:_id}).success(function () {
        $scope.PrintPreviewModal();
    });
}

更新

var _id=1;   
var URL='/api/Pdf/' ;
$scope.EmailPdf = function () {
    $http.get(URL,{id:_id}).success(function () {
        $scope.PrintPreviewModal();
    });
}

错误信息

$id: "1"
 Message: "No HTTP resource was found that matches the request URI 'http://localhost:44301/api/Pdf/'."
   MessageDetail: "No action was found on the controller 'Pdf' that matches the request."

其他选项

  var id=1    
$scope.EmailPdf = function () {
    $http.get('/api/Pdf?id=id').success(function () {
        $scope.PrintPreviewModal();
});
}

错误信息

{"$id":"1","Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'TexasExterior.Controllers.PdfController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}

【讨论】:

  • @user3913120 你的 api 控制器的名称是什么?
  • @user3919120 请查看我的回答顶部更新您的 config.Routes
  • k,我已经用不同的错误消息更新了我的帖子
【解决方案3】:

我唯一需要的是

公共字符串获取(int id=1)

我最初发布的代码不需要更改。不过还是谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多