【问题标题】:POST method in WCF Rest Serivce not working when data is passed in it?WCF Rest Service 中的 POST 方法在传递数据时不起作用?
【发布时间】:2012-02-04 05:48:07
【问题描述】:

当我使用 GET 方法时,我有一个 wcf 休息服务,当我将数据与 url 一起传递时它工作正常,但我需要使用 post 方法,当没有数据传递到 post 方法时它工作正常.. 但是当添加一些数据时,它在 IE 中返回 Bad Request 错误,在 mozila 和 chrome 中它只返回错误。我已经发布了我在下面的 wcf 服务和 ajax 方法中使用的代码。请帮助

Ischeduler.cs 中的代码

[ServiceContract]
public interface Ischeduler
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetData", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetData();

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "GetDataName", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string GetDataName(string Name);
}

scheduler.cs

public string GetData()
    {
        string getdata = "hgello";
        return string.Format("You entered" + getdata);
    }
public string GetDataName(string Name)
    {
        return string.Format("You entered" + Name);
    }

按钮单击时 Schedular.aspx 中的 Ajax 请求

$("#btnInvoke").click(function () {
                  $.ajax({
                      type: "GET",
                      url: "http://localhost:3125/schedulerAPI_Service/scheduler.svc/GetDataName",
                      data: '{"Name": "John"}',
                      dataType: 'json',
                      processdata: true,
                      contentType: "application/json; charset=utf-8",
                      crossdomain: true,
                      success: function (data) {
                         alert(data);
                      },
                      error: function (xhr, status, error) {
                          alert("failed " + error);
                      }
                  });

Wcf 网络配置

<?xml version="1.0"?>
<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
        <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/></system.web>
    <connectionStrings>
        <add name="InstaScribeCentralConnectionString" connectionString="Data Source=ATLT53;Initial Catalog=InstaScribeCentral;User ID=sa;Password=bmjobmjo;" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="ServiceBehavior">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="ServiceBehavior" name="scheduler">
                <endpoint address="" behaviorConfiguration="ServiceBehavior" binding="webHttpBinding" contract="Ischeduler"/>
            </service>
        </services>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>
</configuration>

【问题讨论】:

  • 当我把 jQuery.support.cors = true;在 document.ready 中,我在 IE 中获取数据但在 Mozilla 和 chrome 中出现同样的错误

标签: jquery asp.net ajax wcf rest


【解决方案1】:

像这样传递数据。

data: { Name: "John" }

将类型更改为post 并删除processdata: true 反正有错字,processdata 中的d 应该是大写processData。默认值为true,无需指定。

【讨论】:

  • 是的,我也改变了,但它仍然给出了错误的请求
【解决方案2】:

您是否尝试过像这样在WCF 配置中应用enableWebScript

<endpointBehaviors>
    <behavior name="ServiceBehavior">
        <enableWebScript/>
    </behavior>
</endpointBehaviors>

除此之外,@ShankarSangoli 提到的一切看起来都不错。

【讨论】:

  • 在配置中设置 后出现此错误 --- 使用“UriTemplate”的端点不能与“System.ServiceModel.Description.WebScriptEnablingBehavior”一起使用。
  • 您有两个选择,从WebInvoke 属性中删除UriTemplate 参数或blogs.msdn.com/b/justinjsmith/archive/2008/02/15/…
【解决方案3】:

您必须在 web.config 中将 aspNetCompatibilityEnabled 设置为 false

<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>

如果将 aspNetCompatibilityEnabled 设置为 true,则必须在 ServiceContract 中允许 AspNetCompatibilityRequirements 模式。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

您还必须将类型设置为“POST”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 2018-08-23
    • 2012-03-12
    • 1970-01-01
    • 2014-09-17
    相关资源
    最近更新 更多