Receiving JSON via AJAX in controller function

32 views
Skip to first unread message

Paul Ellis

unread,
May 2, 2019, 3:37:04 PM5/2/19
to web...@googlegroups.com
I have some JS which sends data from a row in a grid to the server as JSON.

The controller function receives the JSON in a request.vars key (not a value). This is awkward to find if there are other request.vars. Currently I am testing for lengths over 10. This was a quick hot fix because I added a request.var which is passed around the app and broke all of the ajax.

Is there a way I can change the JS that the JSON is either:
1 - received as a value in request.vars with a given key so I know exactly where it will be.
2 - received in another way that the goal is still achieved.

I tried to use the JS FormData object type but can't find it server side.

The web2py ajax function almost does what I want, but:
1 - I have to give it a Name rather than an ID. I guess this is because it was built to be used with forms.
2 - What the page does with the returned JSON will become more complex in future.

function updateRow(row, action = 'update'){
   
// console.log('Update Row Called');
   
var poiId = row.prop('id');
   
var qty = row.find('[name="quantity"]').val() || 0;
   
var disc = row.find('[name="discount"]').val() || 0;
   
var mnthDisc = row.find('[name="monthlydiscount"]').val() || 0;
   
var opt = row.find('[name="optional"]').prop('checked');
   
var oid = document.getElementById('oid');
   
var offerId = oid.getAttribute('name');
   
var cid = document.getElementById('cid');
   
var custId = cid.getAttribute('name');
   
    body
= {
       
'action' : action,
       
'cid' : custId,
       
'offer_id' : offerId,
       
'poiId' : poiId,
       
'discount' : disc,
       
'monthlydiscount' : mnthDisc,
       
'optional' : opt,
       
'quantity' : qty,
   
};
   
   
var xhttp = new XMLHttpRequest();
   
    xhttp
.open('POST', '/offer/ajax_update_offer', true);
   
    xhttp
.onreadystatechange = function() {
       
// console.log(this.readyState);
       
// if (this.readyState == 4) {
           
// console.log(this);
       
// };
       
if (this.readyState == 4 && this.status == 200) {
           
var rObj = JSON.parse(this.responseText);
           
if (rObj.status == "failed"){
                location
= rObj.url;
           
};
           
if (rObj.status == "deleted") {
                $
('#'+rObj.poiId).remove();
           
};
            document
.getElementById('purchase_running_total').innerText = rObj.pTotalStr
            document
.getElementById('monthly_running_total').innerText = rObj.mTotalStr
           
var selectedLi = $('.w2p_flash')
            selectedLi
.html(rObj.flash).slideDown({duration: 1000, queue: false})
               
.fadeOut({duration: 2000, queue: false})
               
.promise().done(function() {
                selectedLi
.hide();
           
});
         
       
};
   
};
    xhttp
.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp
.send(JSON.stringify(body));


Reply all
Reply to author
Forward
0 new messages