Dusda
Dusda

Reputation: 3367

Cannot complete a JSONP call from jQuery to WCF

Okay, I am trying (poorly) to successfully make a JSONP call from jQuery on a test page to a WCF web service running locally, as a cross-domain call. I have, at one point or another, either gotten a 1012 URI denied error, gotten a response but in Xml, or just had no response at all. Currently, the way I have it configured it spits back a 1012.

I did not write this web service, so it is entirely possible that I am just missing a configuration setting somewhere, but I've become so frustrated with it that I think just asking on here will be more productive. Thanks guys. Details below.

I have a WCF web service with the following method:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public decimal GetOrderStatusJson(int jobId)

I am trying to call this method from a jQuery test page, via a cross-domain JSONP call.

<script type="text/javascript">
    getJsonAjaxObject(
    "http://localhost:3960/ProcessRequests.svc/json/GetOrderStatusJson",
    { "jobId": 232 });


    function getJsonAjaxObject(webServiceUrl, jsonData) {
        var request = {
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: webServiceUrl,
            data: jsonData,
            dataType: "jsonp",
            success: function(msg) {
                //success!
                alert("blah");
            },
            error: function() {
                //oh nos
                alert("bad blah");
            }
        };


        $.ajax(request);
    }
</script>

Below are the chunks of the web.config I configure for this purpose:

<services>
    <service behaviorConfiguration="MWProcessRequestWCF.ProcessRequestsBehavior"
     name="MWProcessRequestWCF.ProcessRequests">
        <endpoint address="json" behaviorConfiguration="AspNetAjaxBehavior"
         binding="webHttpBinding" contract="MWProcessRequestWCF.IProcessRequests" />
        <endpoint address="" binding="wsHttpBinding" contract="MWProcessRequestWCF.IProcessRequests">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>
</services>
<behaviors>
    <serviceBehaviors>
        <behavior name="MWProcessRequestWCF.ProcessRequestsBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
        <behavior name="AspNetAjaxBehavior">
            <enableWebScript/>
        </behavior>
    </endpointBehaviors>
</behaviors>

Upvotes: 1

Views: 1527

Answers (2)

Adam Butler
Adam Butler

Reputation: 2753

In order to get JSONP to work with jQuery, you'll need to specify your callback like this:

"myurl?callback=?" (jquery documentation here)

Also, the webservice will need to wrap its response with the javascript call. Example: ServiceName.svc/MethodName?jscallback=mycallback

would return

mycallback(JSON STRING);

When I had to implement something like this, I wrote an HttpModule that would watch incoming requests for the "jscallback" querystring key. If the callback was specified in the querystring, then the HttpModule would attach a custom response filter to "wrap" the json as shown above.

Upvotes: 4

John Saunders
John Saunders

Reputation: 161781

[ScriptMethod] is for ASMX web services, not for WCF.

Upvotes: 0

Related Questions