Search code examples
c#jqueryasp.netjsonjsonp

post JSON to asp.net page


I want to post a JSON object to an asp.net page. I have looked all over the web and have seen how to call a web service, or page methods using AJAX to send or retrieve JSON, but I would like to post JSON.

Reason being I would like to dynamically create a PDF document, this PDF document would contain the information that is inside the table.

This is what I tried:

function GetTableValuesAndPost()
{
    try {
        GetAllTableRows("N");// the N stands for not actually submitting to the database same method is used to submit 
                             // for processing and create pdf method.

        var tableValues = '{' + 'w_vendor:[' + w_vendor + "],w_invoice:[" + w_invoice + "],w_invdate:[" + w_invdate + "]," + "w_amount:[" + w_amount + "]," +
                                "w_account:[" + w_account + "]," + "w_fund:[" + w_fund + "]," + "w_org:[" + w_org + "]," + "w_prog:[" + w_prog + "]," + "w_adrsstyp:[" + w_adrsstyp + "]," +
                                "w_adrss_seq:[" + w_adrss_seq + "]," + "w_Row:[" + w_Row + "]," + "w_bank:[" + w_bank + "]," + "w_user:[" + w_user + "]," + "w_addl_info:[" + w_addl_info + "]," + "w_type:[" + w_type + "]" + "}"


        //var tableValues2 = ('w_vendor:' + w_vendor);
        //JSON.stringify({ FWTMP_FIXINV_INV_NUM: FWTMP_FIXINV_INV_NUM })

        //var tableValues = document.getElementById(ParseTable).innerHTML;
        var CreateForm = document.createElement('form');
        CreateForm.setAttribute('method', 'post');
        CreateForm.setAttribute('target', '_blank');
        CreateForm.setAttribute('name', 'pdfgetter');
        CreateForm.setAttribute('id', 'pdfgetter');
        CreateForm.action = "./PDFcreator.aspx";
        var Fld2 = document.createElement('input');
        Fld2.setAttribute('type', 'hidden');
        Fld2.setAttribute('name', 'pdfvalue');
        Fld2.setAttribute('value', tableValues);
        CreateForm.appendChild(Fld2);
        document.body.appendChild(CreateForm);
        CreateForm.submit();
    }

    catch (err) {
        alert(err);
    }

}

This creates the form and posts the data to the intended page. However, the problem that I'm having is that on the receiving end, I end up with a mess of character, like %3245910241630%.

I think this is an encoding issue, so how should I encode this using JavaScript or jQuery?

Any ideas would be helpful and if their is a better way to accomplish this that would also be helpful.


Solution

  • I think that forcing your string to be encoded in UTF-8 would solve this issue.

    You can do that by using the following javascript function.

    function encode_utf8(s) {
      return unescape(encodeURIComponent(s));
    }