[英]ASP.NET / jQuery : post JSON to web-service
我正在嘗試將一些JSON發布到Web服務。 該Web服務已執行,但是沒有可用數據。
jQuery看起來像這樣:
var json = {"Results": results};
var jsonArray=JSON.stringify(json);
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: requestURL,
data: jsonArray ,
dataType: "json",
success: function (data) { TCG.QUE.processSaveButtonSucceeded(data); },
error: function (data) { TCG.QUE.processSaveButtonFailed(data); }
});
在控制器中實現的Web服務如下所示:
public HttpResponseMessage Post([FromBody]string value)
{
object o1 = Request.Content;
HttpResponseMessage r = new HttpResponseMessage(HttpStatusCode.OK);
return r;
}
如果取出[FromBody]
指令,則會得到404。如果將其保留在代碼中,則執行,但是value
參數的值為null。
我以為[FromBody]
指令意味着數據包含在Request對象中,但如果是,我將找不到它。
將不勝感激任何建議,以便我可以在Post
方法內從客戶端訪問JSON。
更新:
我只是重新閱讀了一下: http : //www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api這讓我想知道我的名字是否給JSON Blog客戶端應對應於應用了[FromBody]
的參數的名稱,因此我將參數的名稱從value
更改為Results
但Post
方法中的值仍然為null。
解決方案在閱讀了Prashanth Thurairatnam提到的博客文章后,我將方法更改為如下所示,並且該方法有效:
public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return new HttpResponseMessage(HttpStatusCode.Created);
}
。
不知道您正在傳遞什么results
。 跑到這個博客 。 您可能想嘗試一下。 該博客討論了如何將數據作為JSON對象/數組傳遞(您可能希望在沒有JSON.stringify的情況下嘗試)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.