我开始使用 ASP.NET Web API。当我在控制器中获取我的实体时,我想知道序列化功能,如下所示:
public class EntitiesController : ApiController
{
[Queryable]
public IEnumerable<Entity> Get()
{
return m_repository.GetAll();
}
public HttpResponseMessage Post(Entity entity)
{
if (ModelState.IsValid)
{
m_repository.Post(entity);
var response = Request.CreateResponse<Entity>(HttpStatusCode.Created, entity);
return response;
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
在 JavaScript 方面:
// create new entity.
$.post("api/entities", $(formElement).serialize(), "json")
.done(function (newEntity) { self.contacts.push(newEntity); });
但我不需要实体。我想接收字符串。因此,我以以下方式更改了控制器:
public class EntitiesController : ApiController
{
[Queryable]
public IEnumerable<string> Get()
{
return m_repository.GetAll();
}
public HttpResponseMessage Post(string entity)
{
if (ModelState.IsValid)
{
m_repository.Post(entity);
var response = Request.CreateResponse<Entity>(HttpStatusCode.Created, entity);
return response;
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
我尝试为post 功能dataType
使用不同的( "json"
, "text"
, "html"
) 。和不同的表示, , , . 但我总是在服务器端作为操作中的参数。data
$(formElement).serialize()
"simple Text"
jsonObject
JSON.stringify(jsonObject)
null
entity
Post
我究竟做错了什么?