2 Replies Latest reply on Dec 28, 2012 1:02 AM by Pierre Lotin

    How to pass parameters from erb to .NET Web Service?

    Pierre Lotin

      Hi to all,

       

      Here is my erb, I want to pass a model/json to a .NET Web Service:

       

          toUpload = "jsonContent=" + myModel.find(:all).to_json
      
      
          Rho::AsyncHttp.post(
                :url => "http://myIPAdress/Service1.asmx/myMethod",
                :headers => { 'Content-Type' => 'application/json' },  
                :body => toUpload,
                :callback => url_for(:action => :httpget_callback)    
             )
      

       

      Here is my .NET Web Service:

       

               <WebMethod()> _
              <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
              Public Function myMethod(ByVal jsoncontent As String) As String
                      Debug.WriteLine(jsoncontent)
                      Return "ok"
              End Function
      

       

      I just got @params["status"] == "error"... it works if I remove the line: headers => {'Content-Type' => 'application / json'}

        • Re: How to pass parameters from erb to .NET Web Service?
          Gerbrand Stap

          You need to specify the ScriptService attribute for your .Net webservice (C#):

          [ScriptService]
          public class Service1 : WebService
          {
               [WebMethod]
               public string myMethod()
               {
                    [...]
               }
          }
          

           

          Also make sure that your body contains valid JSON. You use:

          'jsonContent=[data]'

          But valid JSON would be something like:

          '{ "jsonContent" : [data] }'

           

          Try using the to_json method on a hash so you don't have to worry about JSON syntax:

          toUpload = { 'jsonContent' => myModel.find( :all ) }.to_json

           

          Also check the name of the parameter, because I think it is case sensitive (jsonContent vs. jsoncontent).

           

          Good luck!

          1 of 1 people found this helpful
            • Re: How to pass parameters from erb to .NET Web Service?
              Pierre Lotin

              Ok thank you I understand all this!

               

              Imports
              
              <System.Web.Script.Services.ScriptService()> _
              <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
              <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
              <ToolboxItem(False)> _
              Public Class Service1
                      Inherits System.Web.Services.WebService
              
                      <WebMethod()> _
                      <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
                      Public Function testParam1(ByVal p1 As String) As String
                              Debug.WriteLine("testParam1 REACHED " & Now.ToString & " - " & p1)
                              Return "ok"
                      End Function
              
              End Class
              

               

              1) Reach and pass parameter "URL style"

               

                     Rho::AsyncHttp.post(
                        :url => "http://127.0.0.1:1337/Service1.asmx/testParam1",  
                        :body => "p1=TEST",  
                        :callback => url_for(:action => :httpget_callback_upd_articles)    
                     )
              
              

               

              [324024] testParam1 REACHED 28/12/2012 09:56:11 - TEST 
              

               

              2) Reach and pass parameter "JSON style"

               

                     Rho::AsyncHttp.post(
                        :url => "http://192.168.10.107:8081/Service1.asmx/testParam1",
                        :headers => { 'Content-Type' => 'application/json' },    #add this line
                        :body => '{"p1":"TEST"}',  
                        :callback => url_for(:action => :httpget_callback_upd_articles)    
                     )
              
              

               

              [324024] testParam1 REACHED 28/12/2012 10:00:23 - TEST