我正在复制简单类型的模型 https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-1
它工作正常,但我转换(使用& 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?
答案 0 :(得分:0)
如果你的Web Api没有用于Ajax调用,你可以尝试这样的事情:
$('#header').addClass('domaci-hlava');
$(".box-account-links span:contains('Přihlášení')").addClass("prihlaseni-pryc");
$(".box-account-links a:contains('Registrace')").addClass("registrace-pryc");
$( ".prihlaseni-modal" ).load( "https://www.profikotouce.cz/login/ #login-form" );
如果您的Web Api用于Ajax调用,您可以尝试更简单的方法:
return Request.CreateResponse(HttpStatusCode.OK, new StringContent(JsonConvert.SerializeObject(your_object), Encoding.UTF8, "application/json"));
如果需要,您还可以删除Web Api XML Serializer,将return Json(your_object, JsonRequestBehavior.AllowGet);
config.Formatters.Remove(config.Formatters.XmlFormatter);
类Regiser
方法中的WebApiConfig
强制为强制所有答案