Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Vijay 30 posts 47 karma points
    Oct 04, 2010 @ 17:34
    Vijay
    0

    Calling a web service (.svc) from jquery in Umbraco

    Hey guys,

      I need help doing the following.  I searched the forums and couldnt find the correct answer. 

    1. I have a webservice that saves a piece of data and returns JSON, with the values that were saved

    2. This save is called from a jquery script that then uses the return JSON object and displays the values on the screen

    3. The webservice is ready to go

    4. I know how to call a webservice from jquery.  What I dont know is what changes I have to make to the config files to call this webservice.

    Thanks in advance for the help

    Vijay

     

  • Lennart Stoop 304 posts 842 karma points
    Oct 04, 2010 @ 17:55
    Lennart Stoop
    0

    Hi Vijay,

    In my experience you don't need any additional configuration to call a web service.

    What I often do however is add an URL rewriting rule in config/UrlRewriting.config, which allows for clean url's in Jquery:

    <add name="AccountService"

    virtualUrl="^~/account/newpassword"

      rewriteUrlParameter="ExcludeFromClientQueryString"

    destinationUrl="~/webservices/AccountService.asmx/RequestNewPassword" 

    ignoreCase="true"

    />

     

    $.ajax

    ({

    type: "POST",

    url: "/account/newpassword",

    data: ..

    });

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 04, 2010 @ 23:06
    Aaron Powell
    0

    I haven't seen anyone get a WCF service working in Umbraco, only ASMX. It seemed to be some problem with the way WCF hosts itself :/

  • Vijay 30 posts 47 karma points
    Oct 05, 2010 @ 04:08
    Vijay
    0

    @Lennart - Thank you for your quick reply

    @slace - I really hope thats not the case.  Well I am going to try it out tomorrow. I will let you know if it works or not.  If it doesnt work, you think I have no choice but to convert it to a asmx?

  • Vijay 30 posts 47 karma points
    Oct 05, 2010 @ 22:19
    Vijay
    0

    Ok here is what I have so far:

    Webservice

    Imports System.ServiceModel
    Imports System.ServiceModel.Web

    Interface:
    <ServiceContract()>
    Public Interface ITest

        <WebGet(ResponseFormat:=WebMessageFormat.Json)>
        <OperationContract()>
        Function Test(ByVal vFName As String) As ServiceResponse
    End Interface

    Implementation:

    Imports System.Runtime.Serialization

    Public Class Test
        Implements ITest

        Public Function Test1(ByVal vFName As String) As ServiceResponse Implements ITest.Test
            Dim lResponse As New ServiceResponse
            lResponse.x = "Hello my name is: " & vFName
            Return lResponse
        End Function
    End Class

    <DataContract()>
    Public Class ServiceResponse
        <DataMember()>
        Public x As String
    End Class

    ClientCall

            $.ajax({
              type: "POST",
              url: "~/services/MyService.svc/Test",
              data:"{'FName':'vijay'}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function(msg){alert(msg);},
              error:function(msg){alert('failed');}
            });

     

    It alerts "null", but does go to the success function.  What am I missing? ( I even tried msg.x, and thats not working either.)

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Oct 05, 2010 @ 23:02
    Aaron Powell
    0

    Have you put a break point into the service to ensure it is called properly?

    Have you looked into it with something like Charles or Fiddler?

  • Lennart Stoop 304 posts 842 karma points
    Oct 05, 2010 @ 23:26
    Lennart Stoop
    0

    Hi Vijay,

    In ASP.NET 3.5 JSON serialization, the JSON response is encapsulated within a parent object to address a security vulnerability.

    The parent object is a variable named "d". Does alert(msg.d) work for you?

  • Vijay 30 posts 47 karma points
    Oct 05, 2010 @ 23:34
    Vijay
    0

    ok we got it to work.  the reason it was not working is because the page calling this webservice was secure and the webservice itself was not. to fix this we secured our webservice end point in the web.config file.

    This site helped us solve the problem: http://www.myviewstate.net/blog/post/2010/02/26/Make-cross-domain-Ajax-requests-with-jQuery.aspx

     

     

  • Vijay 30 posts 47 karma points
    Oct 06, 2010 @ 00:02
    Vijay
    0

    @Stoop: I am not able to get the rewriting to work....how should the virtualUrl and destinationUrl look if I have a lot of querystring params?

     

    example :

    lets say the actual webservice i want to call is : https://servername/services/service.svc?param1=val1&param2=val2&param3=val3

  • Lennart Stoop 304 posts 842 karma points
    Oct 06, 2010 @ 00:25
    Lennart Stoop
    0

    If you wish to include parameters, try setting the rewriteUrlParameter to "IncludeQueryStringForRewrite".

    I have not tested this one but it should be close to working:

    <add name="MyServiceRedirect"  

    virtualUrl="^https\://servername/myservice"            

            rewriteUrlParameter="IncludeQueryStringForRewrite"

            destinationUrl="https://servername/services/service.svc"

            ignoreCase="true" />

    Calls to https://servername/myservice?param1=val1&param2=val2&param3=val3

    should than be redirected to https://servername/services/service.svc?param1=val1&param2=val2&param3=val3

  • Vijay 30 posts 47 karma points
    Oct 06, 2010 @ 20:49
    Vijay
    0

    Thanks a lot it worked:)

Please Sign in or register to post replies

Write your reply to:

Draft