1

背景:

我创建了一个运行良好的 WCF 服务(.NET 3.5),需要使用 JavaScript 使用该服务,并且由于跨域限制(该服务与包含 javascript 的网页位于不同的服务器上) ,标准的肥皂帖不起作用。我更改了一些配置,根据 Microsoft 博客文章向该方法添加了 WebInvoke 属性,并让服务与 GET 一起工作,并通过使用 soapUI 测试确认该方法正在工作。我试图在 JSFiddle 上设置一个示例,但因为这是一个 Intranet 服务,我显然无法让它工作。

这是我的 SVC.cs 文件中的方法:(我在尝试解决这个问题时对配置文件进行了一些代码更改和更改)

    // On the interface that defines the contract
    [OperationContract]
    [WebGet]
    string Post(string questionid, string answervalue);

    // In my actual service file code behine.
    public string Post(string questionid, string answervalue)
    {

        Guid dataid = _dataProvider.Post(questionid, answervalue);
        _dataProvider.Log(retval.ToString());
        return dataid.ToString();
    }

现在,如果我只输入 URL,我会得到一个表示该值的 GUID 的字符串表示形式。

   http://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009
   returns: "<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">04bb6445-b1af-4214-8f8b-cf66bb15467f</string>"

这是我要开始工作的javascript:

<script type="text/javascript">

    // string functions to allow formatted output
    String.prototype.format = function() {
      var args = arguments;
      return this.replace(/{(\d+)}/g, function(match, number) { 
        return typeof args[number] != 'undefined'
          ? args[number]
          : match
        ;
      });
    };

    $(function() 
    {

        $('.testpostbtn').click(function() {
            $.jsonp({
                url: "ttp://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009",
                callbackParameter: "callback",
                "success" : function(data) {
                   alert(data);

                },
                "error" : function(d, msg) {
                        alert(
                            "Could not post the data\ncallback={0}\ncallbackParameter={1}\nurl={2}\n{3}\n"
                               .format(d.callback, d.callbackParameter, d.url, msg)
                        );
                    }
            });
            alert('request sent!');

        });
    });


</script>

$.jsonp 方法来自:

// jquery.jsonp 2.3.1 (c)2012 Julian Aubourg | MIT License
// https://github.com/jaubourg/jquery-jsonp

我所要做的就是从方法返回的错误,我将其发送到警报框:

Could not post the data
callback=_jqjsp
callbackParameter=callback
url=http://localhost/WebPostSvc.svc/json/Post?questionid=207&answervalue=207009
error

返回的唯一错误消息是“错误”,它不是很有帮助。

我确定我只是错误地使用了该方法,我需要知道:

我究竟做错了什么?

4

1 回答 1

0

1) 要使跨域与 WCF 一起工作,您可能需要配置绑定。

我从http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/复制的示例参见 crossDomainAccessEnabled 属性。

<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="webHttpBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <bindings>
    <webHttpBinding>
      <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
    </webHttpBinding>
  </bindings>
  <services>
    <service name="ServiceSite.CustomersService">
      <endpoint address="" binding="webHttpBinding"
                bindingConfiguration="webHttpBindingWithJsonP"
                contract="ServiceSite.CustomersService"
                behaviorConfiguration="webHttpBehavior"/>
    </service>
  </services>
</system.serviceModel>

2) 使用可用的调试工具。Chrome 内置了很好的开发者工具,在 Firefox 中你可以安装 firebug。这两个工具都可以捕获网络请求并为您提供一些信息。

通常,如果请求从未被触发并且您仍然收到错误,则函数调用有问题,如果请求失败,则服务器上的某些内容正在阻塞。通常您可以查看响应并获取更多信息。如果响应返回正常,但在此之后数据格式有问题则失败。

于 2012-07-02T19:13:14.940 回答