2017-06-09 156 views
1

我想調用的方法ProcessCriteria在AngularJS以下,但由於某種原因,我不斷收到錯誤消息:

VM18010:1 POST (未找到)

下面是我的調用代碼:

var param = { 'Item': item.Key, 'SolrLabel': SolrLabel }; 

     $http({ 
      method: 'POST', 
      url: '/api/TalentPool/ProcessCriteria', 
      data: param 
      //headers: { 
      // 'Content-Type': 'application/x-www-form-urlencoded' 
      //} 
     }).then(function (response) { 
       // success 
       console.log('Facet Data Posted'); 
       return response; 
            }, 
       function (response) { // optional 
        // failed 
        console.log('facet post error occured!'); 
       }); 

我的服務器端方法:

[System.Web.Http.HttpPost] 
     public IHttpActionResult ProcessCriteria(string Item, string SolrLabel) 
     { 

      var itm = Item; 
      var solr = SolrLabel; 

      return Ok(); 
     } 

任何建議嗎?

+0

404表示您試圖發佈的頁面不存在。您可能在網址中存在錯誤。 – Mistalis

+0

你肯定API端點啓動和運行 - 嘗試將簡單的GET方法,看看您是否可以在瀏覽器中點擊它。 –

+0

網址是正確的,因爲我可以通過瀏覽器導航到它。因爲你有你的行動2組的參數和路由器不明白 – ShK

回答

1

ASP.net無法與您的Route Table中的請求相匹配,因爲您的操作中有2個參數,路由器無法理解它。

它預期的參數翹曲這個數據對象。

首先,讓喜歡它型號:

public class Criteria 
{ 
    public string Item { get; set; } 
    public string SolrLabel { get; set; } 
} 

然後改變你的行動:

[System.Web.Http.HttpPost] 
public IHttpActionResult ProcessCriteria(Criteria criteria) 
{ 
    var itm = criteria.Item; 
    var solr = criteria.SolrLabel; 
    return Ok(); 
} 

更新

JSON.stringify更新JavaScript部分:

var param = { 'Item': item.Key, 'SolrLabel': SolrLabel }; 

     $http({ 
      method: 'POST', 
      url: '/api/TalentPool/ProcessCriteria', 
      data: JSON.stringify(param) 
      //headers: { 
      // 'Content-Type': 'application/x-www-form-urlencoded' 
      //} 
     }).then(function (response) { 
       // success 
       console.log('Facet Data Posted'); 
       return response; 
            }, 
       function (response) { // optional 
        // failed 
        console.log('facet post error occured!'); 
       }); 
+0

將其標記爲否定,因爲回覆未回答問題。 – ShK

+0

@ShK你有沒有發現你的問題?你有沒有得到我的答案?你測試過了嗎? :) –

+0

那麼如何從AngularJs $ http傳遞標準對象? – ShK

0

如上回答說由你可以創建一個類,您可以在HTTP POST數據傳遞這樣

var obj = { 
      url: url, 
      async: true, 
      method: 'POST', 
      headers: { 
       "content-type": "application/json; charset=utf-8", 
       } 
      }; 
      if (typeof data != 'undefined' || typeof data != null) { 
       obj.data = data; 
      } 
      $http(obj).then(function(response){ 
      },function(error){ 
      }); 
0

我得到了我的工作,下面是別人的代碼,如果他們卡住就可以了。

var pvarrData = new Array(); 
      pvarrData[0] = JSON.stringify(item.Key); 
      pvarrData[1] = JSON.stringify(SolrLabel); 
      pvarrData[2] = JSON.stringify($localStorage.message); 


       $http({ 
        method: 'POST', 
        url: '/api/TalentPool/ProcessCriteria', 
        data: JSON.stringify(pvarrData), 
        headers: { 'Content-Type': 'application/json' } 
       }).then(function (response) { 
         // success 
         console.log('Facet Data Posted'); 
         return response; 
              }, 
         function (response) { 
          // failed 
          console.log('facet post error occured!'); 
         }); 
相關問題