[英]No MediaTypeFormatter is available to read an object of type 'InventoryItem'
AJAX Call AJAX电话
$.ajax({
url: '/api/Inventory',
cache: false,
type: 'POST',
data: json,
contentType: 'application/json, charset=utf-8',
statusCode: {
201: function (data) {
console.log(data);
viewModel.items.push(data);
}
}
});
Sent Data ( json
) / Request Payload 发送数据(
json
)/请求有效负载
{"Id":0,"Upc":"3456789012","Quantity":"200","Category":"Vodka","TransactionType":"Audit","MetaData":"ABSOLUT 750ml"}
Response Error 响应错误
No MediaTypeFormatter is available to read an object of type 'InventoryItem' from content with media type ''undefined''."
没有MediaTypeFormatter可用于从媒体类型为“未定义”的内容中读取类型为“ InventoryItem”的对象。”
Routed POST method 路由POST方法
public HttpResponseMessage PostItem(InventoryItem item)
All properties in the JSON string are present in the InventoryItem
model. JSON字符串中的所有属性都存在于
InventoryItem
模型中。
A similar question regarding complex types suggested upgrading from Beta to RC to fix a model binding change, which I have done. 关于复杂类型的类似问题建议从Beta升级到RC以修复模型绑定更改,我已经完成了。
If the question isn't obvious, how do I rectify this error? 如果问题不明显,如何纠正此错误? If I add the the [FromUri] attribute to the Routed POST method, then the AJAX call is routed properly, but with an empty
InventoryItem
. 如果将[FromUri]属性添加到Routed POST方法中,则AJAX调用将正确路由,但
InventoryItem
为空。 If you need any other information, please let me know. 如果您需要其他任何信息,请告诉我。
contentType: 'application/json, charset=utf-8',
should be: 应该:
contentType: 'application/json; charset=utf-8',
Notice the usage of ;
注意的用法
;
instead of ,
which is the correct separator between the content type and the charset. 而不是
,
这是内容类型和字符集之间的正确分隔符。 Also if you follow standard RESTful conventions your controller action should be called Post
and not PostItem
as you have shown: 同样,如果遵循标准的RESTful约定,则控制器动作应称为
Post
而不是PostItem
如所示:
public HttpResponseMessage Post(InventoryItem item)
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.