Link to home
Start Free TrialLog in
Avatar of doctor069
doctor069Flag for Canada

asked on

Trying to get WCF to return jsonp

Hi -

I am working with .net 4.0 and trying to get WCF to return jsonp for cross domain support.

The code below returns: {"d":"tester({\"body\":\"Cat\"})"}

I think it needs to return: tester({""body"":""Cat""}) in order for it to work!

I get the jquery error:    invalid label {"d":"tester({\"body\":\"Cat\"})"}

Any help would be appreciated

----web service code---------------
 <OperationContract()> _
   <WebGet(ResponseFormat:=WebMessageFormat.Json)> _
    Public Function HelloGTown() As String
        Dim sss As String = "tester({""body"":""Cat""})"
        Return sss
    End Function

----jquery code ---------

  $.ajax({
            type: 'GET',
    url: 'http://remotedomain.example.com/service.svc/HelloGTown?callback=tester',                          
    dataType: "jsonp",
    jsonpCallback: "tester",
    success: function (msg) {
     alert('OK:');
     },
     error: function (msg) {
       alert('Error');
     }
             
});

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

check the interface section here (things about JSONPBehavior) :  http://jasonkelly.net/2009/05/using-jquery-jsonp-for-cross-domain-ajax-with-wcf-services/
Avatar of doctor069

ASKER

Hi-

Have a look at the. As I understand it this is for .net 2.0. The  .net 4.0 has jsonp support.

using:

 <webHttpEndpoint>
          <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat ="Json" crossDomainScriptAccessEnabled="true"/>
        </webHttpEndpoint>
Avatar of dj_alik
dj_alik

create class

somthing like this
<DataContract> _
Public Class Cat
      <DataMember> _
      Public Property Cat() As String
            Get
                  Return m_Cat
            End Get
            Set
                  m_Cat = Value
            End Set
      End Property
      Private m_Cat As String

End Class


and
<WebGet(ResponseFormat:=WebMessageFormat.Json)> _
    Public Function HelloGTown() As Cat
        Dim cat As Cat = here create instance of CAT with   string Cat property        Return catAs
    End Function
use: http://jsonlint.com/
to validate


change your sss string to something like this:
{
    "tester": {
        "body": "cat"
    }
}

Open in new window

Hi - Same issue...


See code below... returns

{"d":{"__type":"Cat:#","body":"Cat"}}

<DataContract()> _
Public Class Cat
    <DataMember()> _
    Public Property body() As String
        Get
            Return m_body
        End Get
        Set(ByVal value As String)
            m_body = value
        End Set
    End Property
    Private m_body As String

End Class


  <WebGet(ResponseFormat:=WebMessageFormat.Json)> _
    Public Function HelloGTown() As Cat
        Return New Cat() With { _
          .body = "Cat"}
    End Function

Open in new window

Hi - disrupt that example is json not jsond. If I use json on the same domain i works but I am trying to use jsond on a different domain (cross domain support)
you should be able to do something like so:


jQuery.getJSON(url+"&callback=?", function(data) {
    alert(data.Body);
});

Open in new window

you can also give this a shot to simplify things

http://code.google.com/p/jquery-jsonp/
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much!
I spent all day trying to get it working.
My problem was the web.config setup!

I can stop banging my head against the wall!