来自 jquery 的 Restful 服务调用的状态码 405
Posted
技术标签:
【中文标题】来自 jquery 的 Restful 服务调用的状态码 405【英文标题】:Status code 405 for Restful Service call from jquery 【发布时间】:2013-12-16 11:30:12 【问题描述】:我创建了一个安静的服务并尝试从网页调用它。 但我收到错误代码 405。
同样的 rest 服务调用在soap UI 上也能正常工作
下面是 wevservice 和 jquery 调用的详细信息
网络服务接口
[操作合同] [WebInvoke(方法 = “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
);
);
网络配置部分
<?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")
您应该查看这篇 SO 帖子。它与跨域问题有关:Post data to RESTful Invalid HTTP status code 405
【讨论】:
【参考方案2】:尝试在 webHttpBinding 端点的行为配置中添加<enableWebScript/>
,如下例所示。
<system.serviceModel>
<services>
<service name="WCF.TestWCF" behaviorConfiguration="TestWCFBehaviour">
<endpoint address="" binding="webHttpBinding" contract="WCF.ITestWCF" behaviorConfiguration="TestWCFEndPointBehaviour"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestWCFBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="TestWCFEndPointBehaviour">
<enableWebScript/>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
【讨论】:
我使用了 uritemplate,因此在添加 enablewebscript 后会出现错误“使用 'UriTemplate' 的端点不能与 'System.ServiceModel.Description.WebScriptEnablingBehavior' 一起使用。” 你能删除 uri 模板试试吗? 在这种情况下得到“404”:(以上是关于来自 jquery 的 Restful 服务调用的状态码 405的主要内容,如果未能解决你的问题,请参考以下文章
使用带有摘要身份验证、jquery ajax 调用的 RestFul PHP Web 服务
如何从JSP页面调用RestFul Webservice并使用JSON对象?
实现启用 AJAX 的 WCF 服务 - 来自 JQuery 的调用没有响应