none
How to get a json string from an ASPX page into a VB.net function web service RRS feed

  • Question

  • ASPX code:

    Datastr = JSON.stringify('{"data":{"Zip":"' + value1 + '","SKU":"' + value2 + '"}}');
                $.ajax({
                    type:"POST",
                    url:"http://webserver/Service1.asmx/GetCustomers",
                    Data: Datastr,     
                    contentType:"application/json; charset=utf-8",
                    dataType:'json',
                    timeout:30000,
                    success: OnSuccess,
                    failure:function(response){
                        alert(response.d);},
                    error:function(response){
                        alert(response.d);}});
    
    

    <VB.net 2015 code>

    Public Function GetCustomers(DataStr) As List(Of Customer)
    
        Dim ZipTxt = ""
        Dim TxtSKU = ""
        Dim json As JObject = JObject.Parse(DataStr)
    
        ZipTxt = json.SelectToken("DataStr").SelectToken("Zip")
        TxtSKU = json.SelectToken("DataStr").SelectToken("SKU")
    

    I am at a loss as to how to get the string generated in the Ajax script into the VB.net function.

    I need this to work today and after 2 days of working on it, getting rather T'd off.

    Thanks for any help guys!



    Friday, September 8, 2017 4:05 PM

All replies

  • The following code is an example of how I do it.

        Public Shared Function GetMDBConfiguration() As DBConfiguration
            Dim client = New RestClient("https://api.themoviedb.org/3/configuration?api_key=MyAPIKey")
            Dim request = New RestRequest(Method.[GET])
            request.AddParameter("undefined", "{}", ParameterType.RequestBody)
            Dim response As IRestResponse = client.Execute(request)
    
            Dim json As String = response.Content
            Dim status As HttpStatusCode = response.StatusCode
    
            If status <> 200 Then
                Thread.Sleep(10000)
                response = client.Execute(request)
                json = response.Content
            End If
    
            Dim ser As JObject = JObject.Parse(json)
            Dim data As List(Of JToken) = ser.Children().ToList
            Dim output As String = ""
            Dim config = JsonConvert.DeserializeObject(Of DBConfiguration)(json)
            Return config
        End Function
    

    You would want to download NewtownSoft which is what is used for conversion of the json to VB objects.

    To get the definition of the vb objects you can copy the json from the call to the webservice to the clipboard, then open a new class file, then use the Edit/Paste Special to have VS create the class(es) for you.


    Lloyd Sheen

    Friday, September 8, 2017 5:32 PM
  • Thanks Lloyd, I am new to json and API (I usually do desktop stuff).

    In your code, you enter the function, then go to a 3rd party site to gather data in a restful manner.

    I have a web page that has to pass 2 values to a vb.net function.

    type:"POST",
     url:"http://webserver/Service1.asmx/GetCustomers",
     Data: Datastr,


    This script calls the function (it does touch it as coded) but for the life of me I cannot get the function to show the data that is supposed to be posted.


    • Edited by KareninstructorMVP Friday, September 8, 2017 6:13 PM Please when posting code use the code button
    Friday, September 8, 2017 5:47 PM
  • Have you tried to run the debugger on the javascript code? Chrome has a great debugger for this, IE pretty much is worthless for debugging. If using VS2017 you can debug the javascript directly in Visual Studio

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, September 8, 2017 6:18 PM
  • Since this is web development technology I would recommend posting your question to one of the ASP.NET forums:

    https://forums.asp.net/36.aspx/1?Visual+Basic+NET

    https://forums.asp.net/1224.aspx/1?ASP+NET+Web+Pages


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Friday, September 8, 2017 7:57 PM
  • What happens if you just put the URL (http://webserver/Service1.asmx/GetCustomers) into a browser?  

    If it returns json then you don't need  your code just a call to RestRequest as in my sample and retrieve  the json as the content.  Then you can deserialize it into objects for use.


    Lloyd Sheen

    Friday, September 8, 2017 8:06 PM