2017-05-08 72 views
1

我在ASP.Net网页API控制器的方法如下: -HTTP POST

public string addNewTask([FromBody]TaskListModels _newTask) 
     { 
      DataTable _newTaskDataTable = new DataTable(); 
      try 
      { 
       SQLHelper sqlHelper = new SQLHelper(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString.ToString()); 
       sqlHelper.Parameters.AddWithValue("@taskId", _newTask.taskId); 
       //sqlHelper.Parameters.AddWithValue("@userId", userId); 
       sqlHelper.Parameters.AddWithValue("@taskDetails", _newTask.taskDetails); 
       sqlHelper.Parameters.AddWithValue("@companyId", _newTask.companyId); 
       sqlHelper.Parameters.AddWithValue("@dueDate", _newTask.dueDate); 
       _newTaskDataTable = sqlHelper.ReturnDataTableFromStoredProcedure("[dbo].[MYSP]"); 
      } 
      catch (SqlException ex) 
      { 
       Console.WriteLine("SQL ERROR: " + ex.Message); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("ERROR: " + e.Message); 
      } 

      return "String"; 
     } 

凡TaskListModels与某些属性的类,如下所示。

public class TaskListModels 
    { 
     public int taskId { get; set; } 
     public string userId { get; set; } 
     public string taskDetails { get; set; } 
     public int companyId { get; set; } 
     public string companyName { get; set; } 
     public string dueDate { get; set; } 
    } 

我试图从一个角度服务站的一些数据如下 -

public addTask(_newTask: any) { 
console.log('Service - TaskListService : Method - addTask : Params - ' + JSON.stringify(_newTask)) 

this._requestOptions = new RequestOptions({ 
     method: RequestMethod.Post, 
     url: this.addNewtaskApiUrl, 
     headers: this.headers, 
     body: JSON.stringify(_newTask) 
    }); 
    console.log('ADD New - POST Request Query : ' + JSON.stringify(this._requestOptions)); 
    return this._http.request(new Request(this._requestOptions)).map(this._httpExtractDataService.extractData).catch(this._httpErrorHandlerService.handleError); 
} 

如下的数据显示在浏览器控制台,但它不打我的控制器方法 -

POST Request Query : {"method":1,"headers":{"Content-Type":["application/json"]},"body":"{\"taskId\":4,\"taskDetails\":\"Working with Avi for accounting platform project setup\",\"companyId\":4,\"dueDate\":\"2017-05-16\"}","url":"http://localhost:56250/api/Dashboard/addNewTask","withCredentials":null,"responseType":null} 

任何帮助,将不胜感激。

谢谢

+0

你在哪里调用'addTask'? – echonax

+0

addTask是在service.I的方法我在这样的组件称之为 - 公共updateTask(newTask:任何){ \t \t \t this._taskListService.addTask(this.newTask); } –

+0

是否解除了http请求?你在浏览器的网络标签中看到它吗?还有,ASP.NET的'addNewTask',它是否被注册为'POST'端点? –

回答

1

Observable默认情况下是冷的。你需要subscribe来观察实际提出请求:

this._taskListService.addTask(this.newTask).subscribe((res)=>{ 
    console.log(res); 
}); 
+0

谢谢@echonax ...它的工作,非常感谢你。你让我的一天亲爱的... –

+0

@AmitAnand很高兴我能帮忙:-) – echonax

+0

@AmitAnand,因为这个答案对你有帮助,请点击答案下面的灰色滴答声接受答案,以便问题被标记为解决:)这是如何,如果它不清楚:https://meta.stackexchange.com/a/5235 – Alex