jsonp calling WCF returning error

醉酒当歌 提交于 2019-12-13 17:49:28

问题


background:

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.

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();
    }

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

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>

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:

What am I doing wrong?


回答1:


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

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) 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.



来源:https://stackoverflow.com/questions/11246781/jsonp-calling-wcf-returning-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!