简体   繁体   English

如何使用Angular2发送FormParams的帖子

[英]how to send post with FormParams using Angular2

client: 客户:

I do post in angular2 like this: 我发布了angular2这样的帖子:

doSelectMessagesAttributesUrl2(pushRequest : PushRequest) {
    console.info("sending post request");

    let headers = new Headers({
        'Content-Type': 'application/json'});

    return this.http
        .post(this.selectMessagesAttributesUrl, JSON.stringify(pushRequest), {headers: headers})
        .map(res => res.json().data)
        .subscribe(
            data => { },
            err => {  }
        );
}

How should i change the request to call the server with FormParam? 我应该如何更改使用FormParam调用服务器的请求?

server: 服务器:

 @Path("/FeatureGetQueueData")
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public String runFeatureGetQueueData(@FormParam("queue") MyString paramQueue) throws Exception {

        if (!SupporToolAlert.validateEnvironment(SupporToolConfig.ROW)) {
            return SupporToolAlert.invalidEnvironment();
        }

        String queue = PushQueueConfig.conf().QUEUE.get(paramQueue.value);

Did you combine your form with any model ?? 你把表格与任何型号结合了吗?

In documentation you can search nice example: right here 在文档中,您可以搜索好的示例: 就在这里

But if u don't want to create class for your form, u can do something like this: 但是如果你不想为你的表单创建类,你可以这样做:

add each element from form to object, and send this object via http. 将每个元素从表单添加到对象,并通过http发送此对象。

something like this: 这样的事情:

form.html

<form>
 <div class="form-group">
            <label for="status"> Status: </label>
            <input id='status' type="text" class="form-control" [ngModel]="formModel.operatorStatus" (ngModelChange)="changeFormModel('status', $event)" >
          </div>
</form>

now in you component, you need init empty object to contains data from model and implement changeFormModel: 现在在你的组件中,你需要init空对象来包含来自模型的数据并实现changeFormModel:

component.ts

private formModel: any = {};
private changeFormModel(model, event) {
    this.formModel[model] = event;
}

now in your http request u can send, formModel, remember to stringify it ;) 现在在你的http请求你可以发送,formModel,记得将其字符串化;)

Thats not is a perfect solution, but works like charm for me, but in near future i will implement a solution from documentation. 这不是一个完美的解决方案,但对我来说就像魅力一样,但在不久的将来,我将从文档中实现一个解决方案。

I hope it helped you ;) 我希望它能帮到你;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM