1

I'm passing a 2-dimensional array of float values to my views.py via ajax. My ajax call looks like this:

$.ajax({
          method: "POST",
          url: "introURL",
          data: {
            csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
            dsg_mtx : JSON.stringify(dsg_mtx),

           },
          success: function(data) {

            document.getElementById("test").innerHTML = data;  // echo response
          },
          error: function() {
            alert ("Something went wrong");
          }

I'm pulling my data to the view with this call:

def introURL(request):
    if request.is_ajax():
        try:
            designMtx = json.loads(request.POST['dsg_mtx'], parse_float=None)

        except KeyError:
            return HttpResponse('Error, intro import') 

        ... Some transformation of designMtx...


        return HttpResponse(response)       
    else:
        raise Http404   

so my question is how do I convert this json stringify'd object back to a 2-dimensional array which I can use to compute a response? As you can see I tried using the parse_float option, but it's still a str data type.

the array being passed, dsg_mtx, is created with a Handson tables and looks like this:

-1  -1
-1  1
1   -1
1   1

Thanks for your guidance.

4
  • Please show dsg_mtx Commented Nov 6, 2015 at 7:41
  • Added graphical representation of dsg_mtx to my code above. Commented Nov 7, 2015 at 4:24
  • Before $.ajax add console.log(dsg_mtx) and show what is in console. In the same way in Python code before json.loads add print request.POST['dsg_mtx'] and show whats is in output. Commented Nov 7, 2015 at 6:08
  • I think I see the issue... my Handson table is initiated as 2dimensional array of numbers, but as soon as I type anything in a cell it converts the value in that cell to a string. So I'll have to take a look at why the Handson table is behaving this way. Commented Nov 7, 2015 at 15:07

2 Answers 2

1

My issue turned out to be the default settings for handsontable. while I was supplying the table with float values, by default, the table was converting them to strings. so I was posting a matrix of strings that looked like float. the fix was simply to reformat the handsontable cells where the data was placed.

var data = function () {
   return Handsontable.helper.createSpreadsheetData(mtxrows, mtxcols);
  };

  var hot = new Handsontable(container, {
    data: data,
    height: 480,
    colHeaders: true,
    rowHeaders: true,
    stretchH: 'none',
    columnSorting: true,
    contextMenu: true,
    className: "htCenter",
    cells: function (row, col, prop) {
        var cellProperties = {};
        if (row > 0) {
          cellProperties.type = 'numeric';
          cellProperties.format = '0[.]000';
         }                      
        return cellProperties;
        },

    afterChange: function () {
        var tmpData = JSON.parse(JSON.stringify(mtx_data));
    }
  });

So basically anything after the first row is set to numeric type.

so now this line in my ajax call:

dsg_mtx : JSON.stringify(dsg_mtx),

formats it correctly and views.py uses this call to load it in properly:

designMtx = json.loads(request.POST['dsg_mtx'])

Thanks to Tomasz Jakub for suggesting the console.log() which helped me diagnose the issue.

Regards, Jaime

Sign up to request clarification or add additional context in comments.

Comments

0

Try json.loads but in parse_float pass decimal.Decimal

designMtx = json.loads(request.POST['dsg_mtx'], parse_float=decimal.Decimal)

1 Comment

Tried it, but it says that decimal is not defined. after adding: from decimal import Decimal and changing the code to: ... parse_float=Decimal the code runs, but has no impact on the outcome. designMtx is still just a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.