我有一个控制器类,我可以在 API 上发起 Get 调用,但是当我尝试 POST 命令时,我得到 HTTP/1.1 415 Unsupported Media Type
有什么地方我必须允许 POST 吗?我将 [HttpPost] 放在方法前面,但没有运气。
public class initController : ApiController
{
// GET api/<controller>
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<controller>/5
public string Get(int id)
{
return "value";
}
// POST api/<controller>
[HttpPost]
public string Post([FromBody]string value)
{
oTree myT = new oTree();
myT.build(0);
myT.entity.question = value;
return JsonConvert.SerializeObject(myT);
}
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/<controller>/5
public void Delete(int id)
{
}
}
Javascript 邮政编码:
function gpRequest(service, verb, oData, callback) {
if (bool_cantransmit) {
bool_cantransmit = false;
var xdr;
var url = base_service_url + service + "/";
if (window.XDomainRequest) // Check whether the browser supports XDR.
{
xdr = new XDomainRequest(); // Create a new XDR object.
if (xdr) {
xdr.onerror = errorHandler;
xdr.onload = callback;
xdr.contentType = "application/json";
xdr.open(verb, url);
xdr.send(oData);
}
} else {
var xhr = new XMLHttpRequest();
xhr.onerror = errorHandler;
xhr.onload = callback;
xhr.open(verb, url, true);
xhr.send(oData);
}
}
}