Jquery WCF&x2B;JSONP:总是得到一个;“不允许使用方法”;错误消息
在纯javasript/html页面上使用JSONP调用WCF web服务来解决跨域问题,得到一条错误消息“不允许使用方法”,进行了大量在线搜索,但无法获得有效的解决方案 架构:WCF 3.5+JSONP+JQuery 为了在WCF 3.5中启用JSONP功能,我添加了MSDN JSONP示例库文件:Jquery WCF&x2B;JSONP:总是得到一个;“不允许使用方法”;错误消息,jquery,wcf,cross-domain,jsonp,http-status-code-405,Jquery,Wcf,Cross Domain,Jsonp,Http Status Code 405,在纯javasript/html页面上使用JSONP调用WCF web服务来解决跨域问题,得到一条错误消息“不允许使用方法”,进行了大量在线搜索,但无法获得有效的解决方案 架构:WCF 3.5+JSONP+JQuery 为了在WCF 3.5中启用JSONP功能,我添加了MSDN JSONP示例库文件: JSONPBehavior.cs JSONPBindingElement.cs JSONPBindingExtension.cs JSONPEncoderFactory.cs 从。 在这篇文章之
JSONPBehavior.cs
JSONPBindingElement.cs
JSONPBindingExtension.cs
JSONPEncoderFactory.cs
从。
在这篇文章之后:
IService1.cs:
namespace WcfServiceForConnDatabase
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetData")]
[JSONPBehavior(callback = "method")]
string GetData();
}
}
服务中心:
namespace WcfServiceForConnDatabase
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IService1
{
public string GetData()
{
return "Successfully connect with the WCF Web Service";
}
}
}
web.config:
<?xml version="1.0"?>
<configuration>
<!-- WCF configuration -->
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WcfServiceForConnDatabase.Service1Behavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfServiceForConnDatabase.Service1">
<endpoint address="" behaviorConfiguration="WcfServiceForConnDatabase.Service1Behavior" binding="customBinding" bindingConfiguration ="jsonpBinding" contract="WcfServiceForConnDatabase.IService1"/>
</service>
</services>
<bindings>
<customBinding>
<binding name="jsonpBinding" >
<jsonpMessageEncoding />
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
</bindings>
<extensions>
<bindingElementExtensions>
<add name="jsonpMessageEncoding" type="WcfServiceForConnDatabase.JsonpBindingExtension, WcfServiceForConnDatabase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</bindingElementExtensions>
</extensions>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
网络结构如下:
局域网内两台PC机;一个用于客户端javascript页面,另一个用于web服务(192.168.0.23)
希望有人能给我一些建议!!!谢谢大家 > P> >在jQuery Ajax调用(跨域)中使用WCF时必须考虑的事项;
$.ajax({
url: "http://192.168.0.23/Service1.svc/GetData",
type: "GET",
jsonpCallback: "TestData",
contentType: "application/json",
dataType: "jsonp",
error: function () {
alert("Error");
},
success: function (data) {
alert("Success");
}
});
}