尝试发布到 wcf rest 4 服务时出现错误请求

发布于 2024-10-11 13:50:24 字数 1135 浏览 2 评论 0原文

我正在使用 wcf 4.0 Rest 模板并尝试让它与 jquery 一起工作。

我创建了一个新的休息模板项目,并将一个网络表单添加到同一个项目中,只是为了让事情变得简单。

我稍微修改了创建方法,使其看起来像这样

  [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>

然而,fiddler 返回 400 错误,告诉我存在请求错误。我做错了什么吗?

I am playing with the wcf 4.0 rest template and trying to get it to work with jquery.

I have created a new rest template project and added a webform into the same project just to get things simple.

I have slightly modfied the Create Method to look like this

  [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";
    }

Then from my webform I am using this.

 <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>

However fiddler is returning a 400 error telling me there is a request error. Have I done something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-10-18 13:50:24

400 也可能意味着您的服务出现问题。您是否尝试将调试器附加到 Rest 服务?
尝试创建 .Net 控制台应用程序(使用 HttpClient 创建请求)并与您的服务通信?

我遇到了同样的错误,经过半小时的测试,我看到 REST 服务中发生了一些错误。

400 can also mean something in your service went wrong. Did you try to attach a debugger to the Rest-service?
Tried to create a .Net-console application (create the request using HttpClient) and communicate with your service?

I ran into same error, after half hour of testing I saw just some error occured in the REST-service.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文