jquery - 当数据传入时,WCF Rest Service 中的 POST 方法不起作用?

标签 jquery asp.net ajax wcf rest

我有一个wcfrest服务,当我使用GET方法时,当我将数据与url一起传递时,它工作正常,但是我需要使用post方法,当没有数据可以在post方法中传递时,它工作正常。 但是当添加一些数据时,它会在 IE 中返回 Bad Request 错误,在 mozila 和 chrome 中它只会返回 Error。我已经发布了我在下面的 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);
}

调度程序.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 配置

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

最佳答案

像这样传递数据。

data: { Name: "John" }

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

关于jquery - 当数据传入时,WCF Rest Service 中的 POST 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138728/

相关文章:

javascript - 编辑输入值时更改颜色

jquery - 删除表格行并在表格末尾附加相同的表格行

jquery - 如何使用 Jquery 或其他简单操作在创建 View 上隐藏或显示表单组。 ASP.NET MVC 5

c# - 为什么 Azure Active Directory 身份验证会在我的 ASP.NET Core 应用程序中导致字符编码错误?

javascript - 尽管设置了 'No Store',iPad 仍缓存 AJAX 响应

javascript - ajax完整问题

javascript - 如何从迭代中获取 "unset"元素

asp.net - https ://api-secure. recaptcha.net/上的 recaptcha https 问题

java - 如何使用Servlet和Ajax?

jquery - 使用 CSS 将 HTML 转换为 PDF