我正在尝试发布到 EntityFramework 支持的 WCF 数据服务,但我得到:
415 Unsupported Media Type
我按照本指南将标题放入我的 jQuery POST http://blogs.msdn.com/b/astoriateam/archive/2012/04/11/what-happened-to-application-json-in-wcf-ds- 5-0.aspx
这些是我的标题:
POST /webservices/service/service.svc/Activities HTTP/1.1
Host: www.url.com
Connection: keep-alive
Content-Length: 138
Origin: http://www.url.com
X-Requested-With: XMLHttpRequest
MaxDataServiceVersion: 3.0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5
Content-Type: application/json; charset=UTF-8
Accept: application/json;odata=verbose
Referer: http://www.url.com/sites/site/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8,en-GB;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
该站点帮助将 EntityFramework 与 WCF 数据服务一起使用 - http://blogs.msdn.com/b/writingdata_services/archive/2011/06/15/entity-framework-4-1-code-first-and-wcf-data -services.aspx
function AddActivity() {
var activity = {
activity:
{
"Title": "Test From Code",
"Detail": "Code Example",
"Started": "2012-06-21T09:00:00",
"UserId": 17
}
};
var url = 'http://www.url.com/webservices/service/service.svc/Activities';
$.ajax({
type: "POST",
url: url,
data: activity,
dataType: "json",
contentType: "application/json; charset=utf-8",
beforeSend: function (xhr) {
xhr.setRequestHeader("Accept", "application/json;odata=verbose");
xhr.setRequestHeader("MaxDataServiceVersion", "3.0");
},
success: function (data) {
alert('Success');
},
error: function (err) {
alert('Fail\n' + err.statusText);
}
});
}
我可以读取数据 JSON 数据 OK