我正在使用 wcf 4.0 rest 模板并试图让它与 jquery 一起工作。
我创建了一个新的 rest 模板项目,并在同一个项目中添加了一个 web 表单,只是为了让事情变得简单。
我稍微修改了 Create Method 看起来像这样
[WebInvoke(UriTemplate = "", Method = "POST")]
public string Create(SampleItem instance)
{
// TODO: Add the new instance of SampleItem to the collection
return (instance.Id == 1) ? "1 was returned" : "something else was returned";
}
然后从我的网络表单中我正在使用它。
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: 'POST',
url: "/service1/",
data: { "Id": 1,"StringValue": "String content"
},
success: function (data) {
$('.result').html(data);
},
error: function (error) {
$('.result').html(error)
},
dataType: "json",
contentType: "application/json; charset=utf-8"
});
});
</script>
<div class="result"></div>
但是提琴手返回一个 400 错误,告诉我有一个请求错误。我做错了什么吗?