2013-02-08 55 views
10

我似乎無法得到這個工作...我有一些jQuery的像這樣的在客戶端上:jQuery的崗位空的,而不是JSON來的ASP.NET Web API

$.ajax({ 
    type: "POST", 
    url: "api/report/reportexists/", 
    data: JSON.stringify({ "report":reportpath }), 
    success: function(exists) { 
     if (exists) { 
      fileExists = true; 
     } else { 
      fileExists = false; 
     } 
    } 
}); 

在我的網站。 API控制器我有這樣的方法:

[HttpPost] 
public bool ReportExists([FromBody]string report) 
{ 
    bool exists = File.Exists(report); 
    return exists; 
} 

我只是檢查,看是否有文件放在服務器上,併爲返回一個布爾它是否確實與否。我要發送的報告字符串是UNC路徑,所以reportpath看起來像'\\ some \ path \'。

我可以觸發腳本,並在ReportExists方法中創建一個斷點,但報表變量始終爲空。

我在做什麼錯?

我也看到一種方式來張貼.post和postJSON。也許我應該使用其中之一?如果是這樣,我的格式是什麼?

更新:另一個線索可能 - 如果我刪除[FromBody],那麼我的斷點根本就沒有被打 - '沒有找到與請求相匹配的http資源'。我正在看的示例顯示[FromBody]不需要...?

+1

'$ .ajax'會更適合這種情況,因爲您可能還需要包含'contentType:「application/json」'作爲'$ .ajax'選項 – 2013-02-08 19:12:48

+0

請確保檢查您的ModelState是否有錯誤以防在綁定參數時出現問題。 – 2013-02-08 20:40:46

回答

23

所以我發現這個問題,和解決方案。所以,首先。 contentType不能是'application/json',它必須是空白的(默認爲application/x-www-form-urlencoded,我相信)。儘管看起來你必須發送json,但在名稱值對中沒有名稱。使用JSON.stringify也會混淆這一點。因此,完整的工作jQuery代碼是這樣的:

$.ajax({ 
    type: "POST", 
    url: "api/slideid/reportexists", 
    data: { "": reportpath }, 
    success: function(exists) { 
     if (exists) { 
      fileExists = true; 
     } else { 
      fileExists = false; 
     } 
    } 
}); 

在Web.API端,您必須對參數[FromBody] attibute,但除此之外這是很標準的。真正的問題(對我來說)是這個職位。

在提琴手,請求身體看起來像這樣「=%5C%5Croot%5Cdata%5Creport.html」

This後真的有答案,並鏈接到this文章這也是非常有益的。

+0

尼斯鏈接,謝謝:) – 2013-08-05 04:43:20

+0

+1000如果我可以。唯一的職位,完全解決了我的問題。 – 2015-08-31 05:47:03

+0

是的,當你有一個簡單的數據類型,而不使用DTO或模型時,這是有效的。我終於放棄了理論上應該發揮作用的一切,只是完全像這個答案一樣工作。 – JohnWrensby 2016-09-10 13:20:50

10

jQuery.ajax()默認情況下,將contentType設置爲application/x-www-form-urlencoded。您可以改爲發送請求application/json。此外,你應該把你的數據作爲一個字符串,它會得到模型綁定到report參數爲您的文章的方法:

$.ajax({ 
    type: "POST", 
    url: "api/report/reportexists/", 
    contentType: "application/json", 
    data: JSON.stringify(reportpath), 
    success: function(exists) { 
     if (exists) { 
      fileExists = true; 
     } else { 
      fileExists = false; 
     } 
    } 
}); 
+1

是的,內容類型通常是問題。 – 2013-06-20 23:27:09

6

這個工作對我來說,所有其他方法都沒有:

function addProduct() { 
     var product = { 'Id': 12, 'Name': 'Maya', 'Category': 'newcat', 'Price': 1234 };    
     $.ajax({ 
      type: "POST", 
      url: "../api/products", 
      async: true, 
      cache: false, 
      type: 'POST', 
      data: product, 
      dataType: "json", 
      success: function (result) { 

      }, 
      error: function (jqXHR, exception) { 
       alert(exception); 
      } 
     }); 
    } 

Serverside集團:

[HttpPost] 
    public Product[] AddNewProduct([FromBody]Product prod) 
    { 
     new List<Product>(products).Add(prod); 
     return products; 
    } 
+1

我對普通的JSON對象進行了上手操作,不需要stringify! – 2014-06-27 13:05:29

+0

謝謝,真的很感謝,經過多次搜索,終於爲我工作了! – 2014-07-24 08:30:28

0

。員額$服務的宗旨,爲我。從webapi中移除[FromBody]並在jquery客戶端的$ .post的url參數中給出url。有效!

0

如果您使用MVC的FromBody屬性,則MVC資源文件將其視爲可選參數。這意味着即使您只有一個參數FromBody,您也需要使用明確的

你應該能夠這樣簡單的東西的工作:

控制器:

[HttpPost] 
public bool ReportExists([FromBody]string report) 
{ 
    bool exists = File.Exists(report); 
    return exists; 
} 

的Javascript

$.ajax({ 
    type: "POST", 
    url: "api/report/reportexists/", 
    data: { "report":reportpath }, 
    success: function(exists) { 
    ... 

你必須確保你的數據jQuery中的對象與您的Contr的參數名稱相匹配ollers 恰好爲