jsonp调用WCF返回错误 [英] jsonp calling WCF returning error

查看:92
本文介绍了jsonp调用WCF返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我创建了一个运行良好的WCF服务(.NET 3.5),由于跨域限制(使用该服务所在的服务器与包含以下内容的网页位于不同的服务器上),因此需要使用JavaScript来使用该服务javascript),则标准肥皂发布无法正常工作.我更改了一些配置,按照Microsoft博客文章的方法为该方法添加了WebInvoke属性,并使该服务与GET一起使用,并通过对soapUI进行测试来确认该方法在起作用.我试图在JSFiddle上建立一个示例,但是由于这是一个Intranet服务,因此我显然无法使其正常工作.

I had created a WCF service (.NET 3.5) that has been working quite well, A requirement came to consume the service with JavaScript and, due to cross domain restrictions (The service is on a different server than the webpage that contains the javascript), the standard soap post was not working. I changed some configuration, added a WebInvoke Attribute to the method as per a microsoft blog post, and got the service working with GET, and confirmed that the method was working by testing with soapUI. I was trying to set up an example on JSFiddle, but because this is an intranet service, I obviously couldn't get it to work.

这是我的SVC.cs文件中的方法: (我尝试进行一些代码更改以及对配置文件的更改)

Here is the method in my SVC.cs file: (I've made some code changes and changes to the configuration file while trying to figure this out)

    // 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的字符串表示形式.

now if I just type in the URL, I get a string representation of the GUID back that represents that value.

   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:

Here is the javascript that I am trying to get to work:

<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方法来自:

The $.jsonp method is from:

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

我需要处理的只是方法返回的错误,并将其发送到警报框:

All I have to work with is the error returned from the method, which I sent to an alert box:

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

返回的唯一错误消息是错误",它不是很有帮助.

The only error message returned is "error" which isn't very helpful.

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

I am certain that I am just using the method incorrectly, and I need to know:

我做错了什么?

推荐答案

1)要启用使用WCF的跨域功能,您可能需要配置绑定.

1) To enable get crossdomain working with WCF you probably need to configure your bindings.

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

A sample I copied from http://bendewey.wordpress.com/2009/11/24/using-jsonp-with-wcf-and-jquery/ see crossDomainAccessEnabled attribute.

<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.这两个工具都可以捕获网络请求并为您提供一些信息.

2) Use the debugging tools avaiable. Chrome has good developer tools built in and in Firefox you can install firebug. Both these tools can capture the network request and give you some information.

通常,如果从未触发请求,而您仍然收到错误,则函数调用可能有问题,如果请求失败,则服务器上的某些事件将被阻止.通常,您可以查看响应并获取更多信息.如果响应返回确定,但之后失败,则说明数据格式有问题.

Generally if the request is never fired and you still get an error something is wrong with the function call, if the request fails something on the server is blocking. Usually you can look at the response and get more info. If the response return ok but it fails after that something is wrong with the dataformat.

这篇关于jsonp调用WCF返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆