WCF + JSONP: always get a "method not allowed" error message
Working on a pure javasript/html page to call WCF web service using JSONP to workaround cross-domain issue, got an error message "method not allowed", did a lot of searching online, but couldn't get a effective solution..
architecture: WCF 3.5 + JSONP + JQuery
To enable the JSONP feature in WCF 3.5, I added MSDN JSONP sample lib files:
JSONPBehavior.cs
JSONPBindingElement.cs
JSONPBindingExtension.cs
JSONPEncoderFactory.cs
from http://msdn.microsoft.com/en-us/library/cc716898.aspx. following this post: http://jasonkelly.net/2009/05/using-jquery-jsonp-for-cross-domain-ajax-with-wcf-services/
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();
}
}
service.cs:
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>
front-end:
$.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");
}
});
}
the network structure is like this: two PCs in LAN;one for client-end javascript page, the other for web-service (192.168.0.23).
Hope somebody can give me some suggestions!!! appreciate开发者_StackOverflow中文版 you all!
The things that you must consider when consuming WCF in jQuery AJAX call (cross domain);
- Run the WCF Service Project in separate instance of Visual Studio. Do not mix WCF Service Project and Consuming Project in one instance and run at once. When you run the consuming project, the WCF Project must already be up and running.
- Use DataType as jsonp instead of json.
- In web.config of WCF Project, make sure you have the attribute crossDomainScriptAccessEnabled="true" in tag under \\. Also the binding name set to bindingConfiguration attribute in the tag.
Check this out for more information:
Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain)
精彩评论