问题

{
	"readyState": 4,
	"responseText": "{\r\n  \"Message\": \"请求包含实体主体,但不包含 Content-Type 标头。此资源不支持推断的媒体类型“application/octet-stream”。\"\r\n}",
	"responseJSON": {
		"Message": "请求包含实体主体,但不包含 Content-Type 标头。此资源不支持推断的媒体类型“application/octet-stream”。"
	},
	"status": 415,
	"statusText": "error"
}

 

分析

后台API

public IQueryable<Protocol> GetProtocols(ProtocolParam param)

前台代码

$.ajax({
    url: "/api/Protocol",
    type: "GET",
    data: { "type": 2, "isPreferred": true },
    success: function (data) {
        alert(JSON.stringify(data));
    },
    error: function (data) {
        alert(JSON.stringify(data));
    }
});

这样,才出现上面的问题

 

然后查了资料,在前台代码中请求时添加contentType参数

contentType: "application/json", 

这样没有报错了

但是后台获取时,参数都是为空的

后面查了资料,只要在参数前添加FromUri特性即可,前台也不用加contentType

 

解决

只需要在API的参数前添加FromUri,作用是用于传递实体

 

更多推荐

【WebAPI】请求包含实体主体,但不包含 Content-Type 标头。使用FromUri解决