我在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}
任何幫助,將不勝感激。
謝謝
你在哪裏調用'addTask'? – echonax
addTask是在service.I的方法我在這樣的組件稱之爲 - 公共updateTask(newTask:任何){ \t \t \t this._taskListService.addTask(this.newTask); } –
是否解除了http請求?你在瀏覽器的網絡標籤中看到它嗎?還有,ASP.NET的'addNewTask',它是否被註冊爲'POST'端點? –