POST a stringified JSON object to your server instead of HTML POST parameters
Account Home | Help | Blog | Contact us | Log Out


Welcome to Kbytes > Articles

POST a stringified JSON object to your server instead of HTML POST parameters

Posted By: siteadmin on 11/04/2017 08:29:00

Lots of HTML POST parameters?  Like lists of things, maybe unknown quanities of items?  This all starts to get in a bit of a mess.

https://myserver.com/myform.aspx?parameter1=this+data&parameter2=that+data..&parameter12=somemoredata  etc.

Then you've got to manually break it all apart and put it back together again serverside to actually use it.

Much better to use JSON.  Now with Newtonsoft's JSON library on the server you can receive as complicated JSON object as you like and parse it, iterate loops and generally make like easy for yourself.

JSON is native your browser so this is how you create a JSON object there.

    var listOfTables = [];

...
 
                var tableSchema = {
                    "tableName": document.getElementById("schemaEditorTable").rows[k].cells.item(0).innerHTML,
                    "columnNamesCSV": document.getElementById("schemaEditorTable").rows[k].cells.item(4).childNodes[0].title,
                    "tableType": document.getElementById("schemaEditorTable").rows[k].cells.item(1).childNodes[0].value,
                    "emailColforPrimaryTable": emailColforPrimaryTable,
                    "emailHasHTMLorText": emailHasHTMLorText,
                    "emailHasHTMLorTextCol": emailHasHTMLorTextCol,
                    "emailHasHTMLvalue": emailHasHTMLvalue,
                    "emailHasTextvalue": emailHasTextvalue,
                    "emailToNameField": emailToNameField,
                    "hasRelationship": hasRelationship,
                    "relationship": relationship,
                    "singularItemName": singularItemName
                };
 
                listOfTables.push(tableSchema);
            }
 
            var schemaModel = {
                listOfTables: listOfTables
            }
 
            //post it all to server.
            postSchemaModel(schemaModel);
        }
        
So we create a tableSchema object here, and push it onto a list.   This is needed serverside, so we can just POST the whole thing as one of the parameters.  In the code below as modelledSchemaJSON (note the JSON.stringify command to convert it into text format for transmission)
 
 
        function postSchemaModel(schemaModel) {
 
            //document.getElementById('rotatingClock').style = 'display:block';
            //context.Request("mailFromName") , context.Request("mailFromEmailAddress"), context.Request("mailBounceEmailAddress")
            var rUrl = sHost + '/services/DataDBservice.ashx';
 
            var payload = 'userId=' + getCookie('userId') + '&publicKey=' + getCookie('publicKey')
            + '&command=POSTSCHEMAMODEL'
                  + '&mailShortCodeName=' + document.getElementById('mailShortCodeName').value
                    + '&mailFromName=' + document.getElementById('mailFromName').value
                      + '&mailFromEmailAddress=' + document.getElementById('mailFromEmailAddress').value
                        + '&mailBounceEmailAddress=' + document.getElementById('mailBounceEmailAddress').value
                        + '&mailType=' + getParameterByName('mailType')
                  + '&userName=' + getCookie('userName')
            + '&hostName=' + getCookie('hostName')
             + '&modelledSchemaJSON=' + encodeURIComponent(JSON.stringify(schemaModel));
 
            //for ondemand mailTypes we need to include the description
            var typeValue = getParameterByName('mailType');
 
            if (typeValue == 'ONDEMAND' || typeValue == 'STATICLISTS') {
                payload = payload + "&onDemandTemplateName=" + encodeURIComponent(document.getElementById('onDemandTemplateName').value)
            }
 
 
            callXHFpost(rUrl, payload, postSchemaModelHandler);
 
        }

Once you have some demo data, use the amazing http://jsonutils.com/ website to generate a load of Classes in VB.net which will mirror your data.

For example here is the VB.net class for the above code:

Public Class schemaModel
    ' schemaModel is the "fixed/annoted" version that has primary, secondary etc
    'the schemaResponse is similar looking but is the immediate response from the view (and save to db) when the this is JSON of the basic data that is sent by client. 
    Property listOfTables As New List(Of schemaModelTable)
 
End Class
 
Public Class schemaModelTable
 
    Property tableName
 
    Property tableType  'primary/secondary/tertiary
 
    Property columnNamesCSV
 
    Property emailColforPrimaryTable  'these have no reference but are used in the deseraliser
 
    Property emailHasHTMLorText
    Property emailHasHTMLorTextCol
    Property emailHasHTMLvalue
    Property emailHasTextvalue
 
    Property emailToNameField
 
    Property hasRelationship = True    'does it have a relationship with Primary table
 
    Property relationship As New schemaModelRelationship
 
    Property singularItemName
 
End Class
 
Public Class schemaModelRelationship
 
    Property FKtable
 
    Property PKtable
 
End Class

It is then so easy to create a new instance of the class and iterate the list or pass it around in your code as required.  All the properties will appear by intellisense too.

     Dim JSON = context.Request("modelledSchemaJSON")
 
     Dim schemaData = Newtonsoft.Json.JsonConvert.DeserializeObject(Of schemaModel)(JSON)
 
See.. nothing too it.   It is now so easy to add a new varable on the client, and just add a corresponding property on the server JSON object and it will all be transmitted.  Even complex nested object or lists can be added with minimium code change.          

blog comments powered by Disqus

Kbytes Home | Privacy Policy | Contact us | Testing Area

© 2004 - 2018 1 Oak Hill Grove Surbiton Surrey KT6 6DS Phone: +44(020)82617371