GVKun编程网logo

angularjs – 带有null参数的$http.get没有访问Web API控制器(angularjs unknown provider)

11

想了解angularjs–带有null参数的$http.get没有访问WebAPI控制器的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angularjsunknownprovider的相关

想了解angularjs – 带有null参数的$http.get没有访问Web API控制器的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于angularjs unknown provider的相关问题,此外,我们还将为您介绍关于$http 将 AngularJs 放入 WebApi 调用中,将对象的一个属性设为 null、$httpBackend(angularJS)中的URL参数、angulajs angularjs $timeout没有延迟参数的原因、Angular 2 HTTP GET与TypeScript错误http.get(…).map不是一个函数在[null]的新知识。

本文目录一览:

angularjs – 带有null参数的$http.get没有访问Web API控制器(angularjs unknown provider)

angularjs – 带有null参数的$http.get没有访问Web API控制器(angularjs unknown provider)

我想在角度应用程序中使用$http.get尝试使用Web API GET控制器,如下所示:
$http.get(BasePath + '/api/documentapi/GetDocuments/',{
                                    params: {
                                        PrimaryID: ID1,AlternateID: ID2,}
                                }).then( ...

在我的情况下,PrimaryID或AlternateID将具有该值.因此,其中一个将始终为null.

我的Web api方法是

public DocumentsDto[] GetDocuments(decimal? PrimaryID,decimal? AlternateID)
    { ...

当其中一个值为null时,$http.get生成的url如下所示:

http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688

要么

http://BaseServerPath/api/documentapi/GetDocuments/?AlternateID=154

这没有达到我的Web API方法.

但是,如果我使用

http://BaseServerPath/api/documentapi/GetDocuments/?PrimaryID=1688&AlternateID=null

有用.我可以在我的参数中将值硬编码为null,但是我想知道是否有任何正确的方法来实现这一点.

谢谢,
山姆

我从@RobJ得到了正确答案.他发布了答案的链接.我也在这里粘贴相同的答案.解决方案是具有Web API参数的默认值.
public string GetFindBooks(string author="",string title="",string isbn="",string  somethingelse="",DateTime? date= null) 
{
    // ...
}

在我的情况下,它将是

public DocumentsDto[] GetDocuments(decimal? PrimaryID = null,decimal? AlternateID = null)
{ ...

$http 将 AngularJs 放入 WebApi 调用中,将对象的一个属性设为 null

$http 将 AngularJs 放入 WebApi 调用中,将对象的一个属性设为 null

如何解决$http 将 AngularJs 放入 WebApi 调用中,将对象的一个​​属性设为 null?

我是 AngularJS 的新手。我创建了一个对话框来更新一个类对象。除 Id 属性外,所有其他属性都正确。

类:

 [Serializable]
public class Payment
{
    public virtual string Id { get; set; }

    public virtual string PaymentId{ get; set; }

    public virtual string FeeType { get; set; }

    public virtual string Payer { get; set; }

    public virtual string Receiver { get; set; }
 
}

AngularJs 服务

 var updatePaymentData = function (payment) {
        return serviceBase
            .put(serviceBase.applicationserviceWebApiUrl + "api/ExportMaintenance/UpdatePayment",payment)
            .then(function (result) {
                loggingManager.logSuccessMessage("payment " + payment.feeType + " has been updated.");

                return result;
            });
    };

ServiceBase 放置代码:

   function httpRequestWithTimeOut(req,timeOut) {
        var timeOutLength = timeOut;
        var hasTimeOut = false;
        var timeout = $q.defer();
        var deferred = $q.defer();

        setTimeout(function () {
            hasTimeOut = true;
            timeout.resolve();
        },(1000 * timeOutLength));

        // Add the default timeout promise.
        req.timeout = timeout.promise;

        $http(req)
            .success(function (result) {
                deferred.resolve(result);
            })
            .error(function (error) {
                if (hasTimeOut) {
                    deferred.reject({
                        error: "timeout",message: "The request took too long and has time out."
                    });
                } else {
                    deferred.reject(error);
                }
            });

        return deferred.promise;
    } 


 var put = function (location,data) {
        var req = {
            method: "put",url: location,data: data,cache: false
        };

        return httpRequest(req);
    };
 function httpRequest(req) {
        // Default time out to 180 seconds.
        return httpRequestWithTimeOut(req,180);
    }  

WEB API 相关代码:

  // PUT api/ExportMaintenance/UpdatePayment
    [HttpPut,ValidateModel]
    [WebApiClaimsAuthorization(Type = "ExportMaintenance",Value = "CanUpdate",Description = "Can update Export Maintenance")]
    public HttpResponseMessage UpdatePayment(Payment paymentData)
    {
        var responseMessage = new ResponseMessage(Request,paymentData);

        _paymentBusinessLogic.UpdatePaymentData(paymentData,User,responseMessage);

        return responseMessage.SetHttpResponseMessage(paymentData);
    }

enter image description here

放入请求中的数据对象图像

enter image description here

它们是类中的更多属性我在写入时删除了它们所有属性都显示在数据对象图像中 从网络选项卡请求负载

enter image description here

在 WebAPI 中,我将 payment.Id 设为 null,但是当我在 Chrome 调试工具中看到 paymentData 在控制器和服务中具有 Id 值时。

我无法理解为什么 Id 为空。请至少帮我分析一下这个问题。

谢谢, 纳加里。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

$httpBackend(angularJS)中的URL参数

$httpBackend(angularJS)中的URL参数

是否可以在angularJS的$httpBackend中接收URL参数?我想做的事情如下:

$httpBackend.whenGET('/api/v1/users/{userId}').respond(function (method,url,data) {
    // somehow read {userId} and act differently depending on what the value is

    return [200];
});

拥有这个功能真的可以很容易地创建一个功能相当的模拟API(更容易开发前端应用程序而无需运行整个后端).

解决方法

您可以传递正则表达式而不是字符串作为URL.在回调函数中,从URL中提取userId,并回答您想要的内容.

angulajs angularjs $timeout没有延迟参数的原因

angulajs angularjs $timeout没有延迟参数的原因

在几个脚本中我可以找到例如
$timeout(function () {
    $scope.my = 1;            
});

而不是简单

$scope.my = 1;

调用$ timeout没有延迟的目的是什么?

这是一个黑客。 :)但通常的目的是等待到$ digest周期结束,然后将$ scope.my设置为1.超时在所有手表完成后调用。

Angular 2 HTTP GET与TypeScript错误http.get(…).map不是一个函数在[null]

Angular 2 HTTP GET与TypeScript错误http.get(…).map不是一个函数在[null]

我在Angular 2中有一个HTTP的问题。

我只想获取一个JSON列表,并在视图中显示它。

服务类

import {Injectable} from "angular2/core";
import {Hall} from "./hall";
import {Http} from "angular2/http";
@Injectable()
export class HallService {
    public http:Http;
    public static PATH:string = 'app/backend/'    

    constructor(http:Http) {
        this.http=http;
    }

    getHalls() {
           return this.http.get(HallService.PATH + 'hall.json').map((res:Response) => res.json());
    }
}

在HallListComponent中,我从服务调用getHalls方法:

export class HallListComponent implements OnInit {
    public halls:Hall[];
    public _selectedId:number;

    constructor(private _router:Router,private _routeParams:RouteParams,private _service:HallService) {
        this._selectedId = +_routeParams.get('id');
    }

    ngOnInit() {
        this._service.getHalls().subscribe((halls:Hall[])=>{ 
            this.halls=halls;
        });
    }
}

但是,我有一个例外:

TypeError: this.http.get(…).map is not a function in [null]

hall-center.component

import {Component} from "angular2/core";
import {RouterOutlet} from "angular2/router";
import {HallService} from "./hall.service";
import {RouteConfig} from "angular2/router";
import {HallListComponent} from "./hall-list.component";
import {HallDetailComponent} from "./hall-detail.component";
@Component({
    template:`
        <h2>my app</h2>
        <router-outlet></router-outlet>
    `,directives: [RouterOutlet],providers: [HallService]
})

@RouteConfig([
    {path: '/',name: 'HallCenter',component:HallListComponent,useAsDefault:true},{path: '/hall-list',name: 'HallList',component:HallListComponent}
])

export class HallCenterComponent{}

app.component

import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/hall-center.component";
@Component({
    selector: 'my-app',template: `
        <h1>Examenopdracht Factory</h1>
        <a [routerLink]="['HallCenter']">Hall overview</a>
        <router-outlet></router-outlet>
    `,directives: [ROUTER_DIRECTIVES]
})

@RouteConfig([
    {path: '/hall-center/...',name:'HallCenter',component:HallCenterComponent,useAsDefault:true}
])
export class AppComponent { }

tsconfig.json

{
  "compilerOptions": {
    "target": "ES5","module": "system","moduleResolution": "node","sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": false
  },"exclude": [
    "node_modules"
  ]
}

对不起,长的帖子…

我认为你需要导入这:
import 'rxjs/add/operator/map'

或者更一般地说,如果你想有更多的方法观察:

import 'rxjs/Rx';

有关详细信息,请参阅this issue。

今天关于angularjs – 带有null参数的$http.get没有访问Web API控制器angularjs unknown provider的分享就到这里,希望大家有所收获,若想了解更多关于$http 将 AngularJs 放入 WebApi 调用中,将对象的一个属性设为 null、$httpBackend(angularJS)中的URL参数、angulajs angularjs $timeout没有延迟参数的原因、Angular 2 HTTP GET与TypeScript错误http.get(…).map不是一个函数在[null]等相关知识,可以在本站进行查询。

本文标签: