首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >无法将数据保存到Angular2中的api服务器

无法将数据保存到Angular2中的api服务器
EN

Stack Overflow用户
提问于 2017-04-18 15:22:26
回答 1查看 146关注 0票数 0

尝试插入数据到web api服务器使用angular 2和services.here我不能插入任何数据.Cant检测问题..can任何人帮助?谢谢

我的html

代码语言:javascript
代码运行次数:0
运行
复制
          <h1>Angular2 POST DEMOh1>

                 <hr>
                  <ul>
                 <p>id <input type="text" name="ID" [(ngModel)]="ID">
             <p>Parameter 1 <input type="text" name="param1" 
      [(ngModel)]="param1">
    <p>Parameter 2 <input type="text" name="param2" [(ngModel)]="param2">
           <p>formula 1 <input type="text" name="param3" 
             [(ngModel)]="param3">
              <p>formula 2 <input type="text" name="param4" 
           [(ngModel)]="param4">
             <p>startdate <input type="text" name="param5" 
            [(ngModel)]="param5">
            <p>enddate <input type="text" name="param6" 
             [(ngModel)]="param6">


             <button 
    (click)="SendToApi(ID,param1,param2,param3,param4,param5,param6)">
          Save
           </button></p>
            </ul>

我的服务:尝试使用angular 2和服务将数据插入web api服务器

代码语言:javascript
代码运行次数:0
运行
复制
  import {Injectable} from "@angular/core";
  import { Http, Response, Headers, RequestOptions} from "@angular/http";
  import {Observable} from "rxjs/Rx";
  @Injectable()

   export class DemoService {
  constructor(private _httpurl:Http){

       }
            AddDetails(params) {
             let headers = new Headers({ 'Content-Type': 'application/json' 
        });
        let options = new RequestOptions({ headers: headers });
            let body = JSON.stringify(params);
          return this._httpurl.post
  ('http://uraxapiservicepoc2test.azurewebsites.net/api/Test ', body, 
headers)
.map((res: Response) => res.json());
 }

}

我使用angular 2和服务将数据插入到web api服务器的component.ts:trying

代码语言:javascript
代码运行次数:0
运行
复制
   import { Component, OnInit } from '@angular/core';
   import {Observable} from "rxjs/Rx";
      import {DemoService} from './DemoService.service'; 
          import { Http, Response, Headers, RequestOptions} from 
         "@angular/http"; 



       @Component({
          selector: 'my-app',
             templateUrl:'../app/app.component.html',
          providers:   [DemoService]        
              })

   export class AppComponent  {


     public ID;
        public param1;
          public param2;
             public param3;
           public param4;
           public param5;
               public param6;


        constructor( private demoservice:DemoService){}

    SendToApi(ID,param1,param2,param3,param4,param5,param6) {
      console.log(ID,param1,param2,param3,param4,param5,param6);
      let data = {ID:ID,param1:param1,
       param2:param2,param3:
        param3,param4:param4,param5:param5,param6:param6};
          this.demoservice.AddDetails(data).subscribe(

          error => {
              console.error("Error saving params!");
              return Observable.throw(error);
               }
               );
             }
                }
EN

回答 1

Stack Overflow用户

发布于 2017-04-18 17:07:33

代码语言:javascript
代码运行次数:0
运行
复制
// this code is in your service 
this._httpurl.post("http://uraxapiservicepoc2test.azurewebsites.net/ ", 
 body, options)
       .map((response:Response) => response.json());
 // this code used to get data 
this.dataService.doSignUp(body).subscribe(result => {

});

你可以试试上面的代码

我希望它能对你有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43466046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档