2016-06-14 56 views
1

我想在角度2中使用HTTP服務的http post請求。但參數不會在web api動作上進行。下面是我的組件和服務:
我的部件,其中服務注入(LogService)從Angular2到WebApi的HTTP POST

import {Component, Inject} from '@angular/core'; 
    import {Http, HTTP_BINDINGS} from '@angular/http'; 
    import {Observable} from 'rxjs/Rx'; 
    import {LogService} from '../../services/canvas/logService' 
    import {PagingModel} from '../../PagingModel'; 
    @Component({ 
    selector: 'about', 
    templateUrl: 'app/components/about/about.html', 
    styleUrls: ['app/components/about/about.css'], 
    bindings: [LogService, HTTP_BINDINGS], 
    providers: [], 
    directives: [], 
    pipes: [] 
    }) 

    export class About { 
     private abc = ""; 
     constructor(@Inject(LogService) public _logService: LogService) { 
     this._logService = _logService; 
    } 

    ngOnInit() { 
    this.getAllLogs(); 

    } 
    getAllLogs() { 
    var body = { CurrentPageIndex: 1, PageSize: 10,SearchText:"JHFGHY"}; 
    let mod = new PagingModel(); 
    mod.CurrentPageIndex = 1; 
    mod.PageSize = 10; 
    mod.SearchText = "sdfs"; 
    this._logService.getAllLogs(mod).subscribe(function (data) { 

     var abc = data.json(); 
    }); 


    } 
} 

服務從那裏數據被髮布到網頁API後

import { Injectable } from '@angular/core'; 
import { Http, Response, Headers, RequestOptions} from '@angular/http'; 
import {Observable} from 'rxjs/Observable'; 
import 'rxjs/add/operator/map'; 
import {About} from '../../components/about/about'; 
require('rxjs/add/operator/toPromise'); 


@Injectable() 
export class LogService { 
public headers: Headers; 

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

} 
    getAllLogs(model): Observable<Response> { 

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

    return this.http.post("http://localhost:64224/api/Logs/Paging",JSON.stringify(model), options); 

    } 


} 

(LogService)和我的網頁API控制器動作是:

[HttpPost] 

    [Route("Paging")] 
    public ResponseMessage Paging([FromBody] PagingModel model) 
    { 
     ResponseMessage responseMessage = new ResponseMessage(); 
     List<ActivityLogModel> activityLogModelList = new List<ActivityLogModel>(); 
     activityLogModelList = logService.Paging(model); 
     responseMessage.totalRecords = activityLogModelList.Count(); 
     activityLogModelList = activityLogModelList.OrderBy(x => x.UserId).Skip((model.CurrentPageIndex - 1) * model.PageSize).Take(model.PageSize).ToList(); 
     responseMessage.data = activityLogModelList; 
     return responseMessage; 
    } 
    #endregion 

我無法從此Web Api控制器中的LogService http post方法獲取參數。請幫我在這裏。謝謝你在前進

+0

您能告訴更多的關於發送的請求(從開發工具)的內容?謝謝! –

回答

1

getAllLogs應該

getAllLogs() { 
    var body = { CurrentPageIndex: 1, PageSize: 10,SearchText:"JHFGHY"}; 
    let mod = new PagingModel(); 
    mod.CurrentPageIndex = 1; 
    mod.PageSize = 10; 
    mod.SearchText = "sdfs"; 
    this._logService.getAllLogs(mod).subscribe(data => { // <<<=== use arrow function instead of `function` 
    this.abc = data.json(); // <<<=== this.abc 
    }); 
} 
+0

他正在談論他的控制器在C#Webservice部分。準確地說是 – rinukkusu

+0

。請幫助我將參數發送到web api操作。非常感謝GünterZöchbauer –

+0

對不起,不能幫助這裏。如果您修正了我的建議,我可以刪除我的答案。 –