Results 1 to 2 of 2
  1. #1

    Thread Starter
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Cape Town

    Create JSON to send to server

    I managed to create something where I have an set of hidden inputs (all share the same class name)

    The "id" of each is the date in a format of "yyyy_mm_dd".
    The value of each input is empty.

    When the user check a checkbox next to each entry (date) I bring up a dialog (text area), allow the user to enter something and then insert that text to the value of the hidden input.

    When one "submit" the form (I prevent it though), I want to build up some kind of "array" of dates (that silly format I use as Id is ok) and and the text (if any) the user entered for that day and post it as JSON to a server side controller action (I used Asp.Net MVC)

    I can get all the values I require now in my JavaScript code, but not sure how to build up a JSON object and have my controller action be able to process it though.

    Any online sample would do the trick, unless someone feel like explaining.

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004

    Re: Create JSON to send to server

    This uses jQuery ajax to submit a JAVASCRIPT object - converted to a JSON string - to a web service

    function ctrlWebService(strOpt, strVal1, strVal2, strId) {
        var objWebParam = {};
        objWebParam.username = window.username || "";
        objWebParam.ctrloption = strOpt;
        objWebParam.ctrlval1 = strVal1;
        objWebParam.ctrlval2 = strVal2;
        var strWebParam = $.toJSON(objWebParam);
            type: "POST",
            url: "WebService.asmx/CtrlService",
            dataType: "json",
            data: strWebParam,
            contentType: "application/json; charset=utf-8",
            success: function(msg) {
                ajaxCtrlFinished(msg, strId, {});
            failure: function(msg) {
                ajaxCtrlFinished(msg, "failure", {});
            error: function(msg) {
                ajaxCtrlFinished(msg, "error", {});
    The webservice is declared like this

        <WebMethod()> _
         <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
         Public Function CtrlService(ByVal ctrloption As String, ByVal ctrlval1 As String, ByVal ctrlval2 As String, ByVal username As String) As String
    If one of the object pairs was more complex then just a string value you can have the web service declared in these other various ways

    Public Function AddService(ByVal toddtype As String, ByVal fromddtype As String, ByVal fromwho As String _
    , ByVal objReturn As Dictionary(Of String, String) _
    objReturn is a OBJECT datatype from the client side...

        Public Function OperatorService(ByVal toddtype As String, ByVal fromddtype As String, ByVal fromwho As String _
                                                      , ByVal newkeys As IList(Of String) _
                                                      , ByVal updkeys As IList(Of String) _
                                                      , ByVal editkey As String _
                                                      , ByVal username As String _
                                                      , ByVal objReturn As Dictionary(Of String, String)) As String
    newkeys and updkeys are ARRAYS of strings from javascript client side

    , ByVal source As IList(Of Dictionary(Of String, String))
    source is an ARRAY of JS OBJECTS.

    That pretty much covers all the datatype combinations in JAVASCRIPT - all coming in as nice handy to use datatypes in VB.

    Hope that was your question!!!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.