1

I'm setting up a page where our staff can view a list of items that should be on the shelf and then confirm if the items are there, or send in an alert if there's a problem.

I'm trying to create one function for each situation (item present, item not present, information incorrect) where each function will pass the correct parameters for that situation to a generic function which will make the actual ajax request.

This function is triggered by a button click (when the item is present and correct) and it logs the data correctly:

function log (rec_id,offset) { console.log("On Shelf \r\nRecord_Id: "+rec_id+"\r\nOffset: "+offset); var rdata =[]; rdata['id'] = rec_id; rdata['status'] = 1; rdata['branch'] = $("#branch").val(); rdata['loc_code'] = $("#location_code").val(); console.log("log function"); console.log(rdata); log_item(rec_id,offset,rdata); } 

this is what is output to the log:

[id: 1833049, status: 1, branch: "1", loc_code: "mnlp"] 

the log_item function makes the ajax request

function log_item(rec_id, offset, rdata) { console.log("Logging Item: i"+rec_id+"a"); console.log("log_item function"); console.log(rdata); $.ajax({ url: 'logItem.php', data: rdata }).done(function(response){ console.log(response); $("#form").foundation('close'); }); } 

It too logs the data correctly

[id: 1833049, status: 1, branch: "1", loc_code: "mnlp"] 

But when I view the requests sent, these parameters are not getting passed in request.

8
  • Specify method: "POST" in the Ajax request Commented Apr 15, 2016 at 16:38
  • Is there a reason GET won't work? I wrote logItem.php to retrieve the parameters from $_GET ? Commented Apr 15, 2016 at 16:40
  • 1
    you can't send json data via Get method Commented Apr 15, 2016 at 16:41
  • Shouldn't it be an object instead of an array? I don't think [] is going to be passed as an object. Commented Apr 15, 2016 at 16:44
  • 1
    @misher jQuery converts its to a query string and appends it to the URL. api.jquery.com/jQuery.Ajax Commented Apr 15, 2016 at 16:45

1 Answer 1

3

i think the problem is here.

var rdata =[]; 

Change it to

var rdata ={}; 
Sign up to request clarification or add additional context in comments.

3 Comments

That did the trick. From what I understand, that changes it to an object instead of an array, any idea why the array didn't work?
javascript does not support associative array. array can have only index like 0,1,2 .(always numeric). For storing key value js has object.
@cdm014 The jQuery ajax function uses jQuery param to convert the data into the query string for a GET request: api.jquery.com/jquery.param. As you can see from the documentation, it supports array as an input type, but you passed in an empty array (length == 0), so it produced nothing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.