WCF + JSONP:总是得到“不允许的方法”错误信息

发布于 2024-12-09 11:46:09 字数 3491 浏览 0 评论 0原文

在纯javasript/html页面上使用JSONP调用WCF Web服务来解决跨域问题,收到错误消息“方法不允许”,在网上进行了大量搜索,但无法得到有效的解决方案

.. :WCF 3.5 + JSONP + JQuery

为了启用 WCF 3.5 中的 JSONP 功能,我添加了 MSDN JSONP 示例库文件:

JSONPBehavior.cs
JSONPBindingElement.cs
JSONPBindingExtension.cs
JSONPEncoderFactory.cs

来自 http://msdn.microsoft.com/en-us/library/cc716898.aspx。 在这篇文章之后: 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";
        }
    }
}

网络配置:

<?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>

前端:

$.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");
            }
        });

    }

网络结构是这样的: 局域网中的两台 PC;一台用于客户端 javascript 页面,另一台用于 Web 服务(192.168.0.23)。

希望有人能给我一些建议!谢谢大家!

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 you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2024-12-16 11:46:09

jQuery AJAX 调用中使用 WCF 时必须考虑的事项(跨域);

  1. 在 Visual Studio 的单独实例中运行 WCF 服务项目。不要将 WCF 服务项目和消费项目混合在一个实例中并同时运行。当您运行使用项目时,WCF 项目必须已启动并正在运行。
  2. 使用 DataType 作为 jsonp 而不是 json。
  3. 在 WCF 项目的 web.config 中,确保 \\ 下的标记中有属性 crossDomainScriptAccessEnabled="true"。还将绑定名称设置为标记中的 bindingConfiguration 属性。

查看此以获取更多信息:

在不同项目中通过 AJAX 调用使用 jQuery 中的 WCF 服务(跨域)

The things that you must consider when consuming WCF in jQuery AJAX call (cross domain);

  1. 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.
  2. Use DataType as jsonp instead of json.
  3. 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文