我最近在刷我的jQuery时遇到了一个问题,当我越过帖子时,我无法得到来自wcf服务的响应我经常得到405 - 方法不允许。我的要求对我来说很好,我想知道我是否错过了一些至关重要而又明显的原因。
这是使用的邮政编码:
$.ajax({
type: "POST",
url: "http://localhost:59929/CustomerService/GetCustomers",
data: null,
ContentType: "application/json",
dataType: "json",
success: function (msg) {
alert("Called and got: " + msg);
},
error: function (result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
}
});
wcf代码如下:
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
List<Customer> GetCustomers();
[OperationContract]
OperationStatus InsertCustomer(Customer cust);
}
配置如下:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Fiddler将原始帖子显示为:
POST http://localhost:59929/CustomerService/GetCustomers HTTP/1.1
Host: localhost:59929
Connection: keep-alive
Content-Length: 0
Origin: http://localhost:59513
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.60 Safari/537.1
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:59513/LearnJQuery2/ajax/ajax_post.htm
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
并且小提琴手也确认了405响应。
答案 0 :(得分:1)
您拨打的是哪个域名?它可能由Same origin policy引起。如果是这种情况,请尝试使用JSONP而不是JSON。
另外,您是否尝试过像Fiddler这样的工具来确切了解请求/响应是什么?这可以为发生的事情提供一些启示。
答案 1 :(得分:1)
好吧,我看了很长一段时间,然后意识到这不是一件坏事,而是一个令人痛苦的事情:)所以这件问题已经解决了。
他们所有的拳头必须确实来自同一个域,并且具有相同的端口和协议,但事实并非如此。我将我的服务移动到我的应用程序中并适当地配置了绑定。下一部分是正确装饰您的WCF服务,因此这里是正确配置服务的代码。
ICustomerService.cs
[ServiceContract]
public interface ICustomerService
{
[OperationContract]
[WebInvoke(
Method = "POST" ,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json)]
List<JSONCustomer> GetCustomers();
}
CustomerService.cs
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Allowed)]
public class CustomerService : ICustomerService
{
public List<JSONCustomer> GetCustomers()
{
return new List<JSONCustomer>
{
new JSONCustomer {id = 1, FirstName = "john", LastName = "Doe"},
new JSONCustomer {id = 2, FirstName = "jane", LastName = "Doe"},
};
}
}
的Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehavior"
name="CustomerService">
<endpoint address=""
binding="webHttpBinding"
contract="ICustomerService"
behaviorConfiguration="EndpBehavior"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
接下来是使用的ajax代码(整个网页):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax Post</title>
<script type="text/javascript" src="../scripts/jquery-1.7.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#HelpButton').click(function () {
$.post('../CustomerService.svc/GetCustomers', null,
function (data) {
var custs = data["GetCustomersResult"];
var text = '';
$(custs).each(function () {
text += '<span>' + this.FirstName + ' ' + this.LastName + '</span><br/>';
});
$('#OutputDiv').html(text);
}
, 'json');
});
});
</script>
</head>
<body>
<input id="HelpButton" type="button" value="Press me"/>
<div id="OutputDiv" />
</body>
</html>
JSONCustomer.cs
[DataContract]
public class JSONCustomer
{
[DataMember]
public int id { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
我真的希望那些现在遇到问题的人会在这方面找到帮助,重要的是你要注意jquery中的所有东西,绑定,装饰和ajax代码,并且它只是赢得了工作
答案 2 :(得分:0)
这看起来像服务器配置问题,调用者无法将该方法视为wcf服务的一部分。此链接提供了一些有用的提示,说明如何解决405错误的最常见原因。我希望它有所帮助:http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/31d3f1aa-28b6-4bd7-b031-73b7e7588e6d/