WCF Rest Service中的POST方法在传入数据时无法正常工作?

时间:2012-02-04 05:48:08

标签: jquery asp.net ajax wcf rest

当我使用GET方法时,我有一个wcf rest服务,当我将数据与url一起传递时它工作正常,但是我需要使用post方法,当post方法中没有数据传递时它工作正常.. 但是当添加一些数据然后它返回IE中的错误请求错误并且在Mozila和chrome中它只返回Error.I已经发布了我在wcf服务和ajax方法中使用的代码.pls help

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

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

3 个答案:

答案 0 :(得分:0)

传递这样的数据。

data: { Name: "John" }

将类型更改为post并删除processdata: true无论如何都有拼写错误,processdata中的d应为大写processData。它的默认值为true,因此无需指定。

答案 1 :(得分:0)

您是否尝试在WCF配置中应用enableWebScript,如下所示:

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

除此之外,@ ShankarSangoli提到的东西一切都很好看。

答案 2 :(得分:0)

您必须在web.config

中将aspNetCompatibilityEnabled设置为false
<serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>

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

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

您还必须将类型设置为&#34; POST&#34;。