我複製的 https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1如何從MVC Web API接收json參數?
它工作正常,簡單型的模式,但我得到轉換(與&qout;)JSON的結果就像
type: 'POST', url: 'api/updates/simple', contentType: 'text/x-json; charset=utf-8', processData: false, dataType: 'json', data: { 'FirstName': 'Anthony' }
這裏是我用來發送請求的AJAX:
function simplemsg() {
$.post("api/updates/simple",
{
"": "type: 'POST', url: 'api/updates/simple', contentType: 'application/json; charset=utf-8', processData: false, dataType: 'json', data: { 'FirstName': 'Anthony' }"
}, function (data) {
console.log(data);
});
}
這裏是UpdatesController.cs:
static readonly Dictionary<Guid, Update> updates = new Dictionary<Guid, Update>();
[HttpPost]
[ActionName("Simple")]
public HttpResponseMessage PostSimple([FromBody] string value)
{
if (value != null)
{
Update update = new Update()
{
Status = HttpUtility.HtmlEncode(value),
Date = DateTime.UtcNow
};
var id = Guid.NewGuid();
updates[id] = update;
var response = new HttpResponseMessage(HttpStatusCode.Created)
{
Content = new StringContent(update.Status)
};
response.Headers.Location =
new Uri(Url.Link("DefaultApi", new { action = "status", id = id }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
我試過IActionResult,但它似乎在Visual Studio 2015中未定義 如何在Web API中接收真正的Json?
首先,它看起來不像你使用jQuery post,因爲它的目的是...... [refrence](https://api.jquery.com/jquery.post/)。它看起來像你試圖做的是jQuery.ajax()作爲post()應該被用作.ajax()的簡寫方法。 – fjoe
請檢查我的帖子 –
我想你也可以改變你的回覆回覆;進入回報Json(迴應); – fjoe