jquery - 来自 jquery 的 Restful 服务调用的状态代码 405

标签 jquery ajax wcf restful-url

我创建了一个安静的服务并尝试从网页调用它。 但我收到错误代码 405。

相同的休息服务调用在肥皂 UI 上工作正常

下面是 wevservice 和 jquery 调用的详细信息

网络服务 API

[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "PostMethodWithCustomerArray")] List PostMethodWithCustomerArray(List dataStrings);

网络服务代码

    public List<CustomerWithArray> PostMethodWithCustomerArray(List<CustomerWithArray> dataStrings)
    {
        return dataStrings;
    }

ajax调用

var UrlPostMethodWithCustomerArray = "http://localhost:29745/RESTfulService.svc/PostMethodWithCustomerArray";

    var customerWithArray = [{ "Name": "Dipak Godbole", "Age": "27", "HumanType": "1", "strArray": ["11", "22"] },
   { "Name": "Madan Chapa", "Age": "19", "HumanType": "3", "strArray": ["111", "222"] },
   { "Name": "Raju Rohida", "Age": "55", "HumanType": "2", "strArray": ["1111", "2222"] }];

$(document).ready(function () {
    $.ajax({
        cache: false,
        type: "POST", //GET or POST or PUT or DELETE verb
        url: UrlPostMethodWithparam, // Location of the service
        data: JSON.stringify(customerWithArray),
        dataType: "json", //Expected data format from server
        contentType: "application/json; charset=utf-8",
        success: function (msg) {//On Successfull service call

            $.each(msg, function (index, item) {
                alert(item.Name + item.HumanType);
            });
            //alert(msg);
        },
        error: ServiceFailed// When Service call fails
    });
});

Web 配置部分

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="TrialRESTService.RESTfulService" behaviorConfiguration="svcbehaviour">  
        <endpoint address="" 
                  binding="webHttpBinding" 
                  contract="TrialRESTService.IRESTfulService"/>
      </service>
    </services>
    <behaviors>
    <serviceBehaviors>
        <behavior name="svcbehaviour">
          <serviceMetadata httpGetEnabled="true"/>
         <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
        </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="POST,GET"/>
      </customHeaders>
    </httpProtocol>
  </system.webServer>



</configuration>

有什么帮助吗???

使用 Google crome 开发者工具时出错

    OPTIONS 
    http://localhost:29745/RESTfulService.svc/PostMethodWithCustomerArray 
   405 (Method Not Allowed) jquery-1.8.2.min.js:19

 XMLHttpRequest cannot load     http://localhost:29745/RESTfulService.svc/PostMethodWithCustomerArray. Invalid HTTP status code 405 Default.aspx:1

最佳答案

这对于您的数据有效负载来说是不正确的(不是有效的 json):

data: JSON.stringify("Dipak Godbole")

相反,你应该有

data: JSON.stringify("{name: Dipak Godbole}")

你应该看看这个帖子。它与跨域问题有关:Post data to RESTful Invalid HTTP status code 405

关于jquery - 来自 jquery 的 Restful 服务调用的状态代码 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609839/

相关文章:

javascript - 这个 jquery 代码不起作用?

jquery - 选择不是当前元素/JQuery 父级的先前标题

javascript - 将 Jquery 中的值列表发送到 flask 模板

wcf - 完全禁用 WCF 代理

javascript - Google 跟踪代码管理器 - 存储单击的 div/按钮的 ID

javascript - 如何将以前的功能添加到简单的 jQuery slider

ajax - 我应该在服务器端还是客户端做 API 请求?

javascript - jQuery Ajax 方法成功,但没有收到数据

asp.net - WCF 数据服务查询 URL 中 DateTime 的 $filter 选项

wcf - 如何强制 IIS 托管的 WCF 或 ASMX [webservice] 以只读方式使用 session 对象?