In the process of working on a script, I encountered a situation where an $.ajax call had to invoke a function upon success, returning a JavaScript object in JSON format [object object]
. However, despite being able to return a well-formatted string, accessing the object's properties proved to be challenging. Here is the structure of the actual object:
{"id":"12","created_at":"2015-10-30T21:30:44.457Z",
"card":{"id":"67","number":"89990","expires":"2018"},
"verification":"required"}
This nested structure resembles an object within another object in JavaScript, with the outer object named employee and the inner one called card. Despite my efforts, I am unable to comprehend what mistake I might be making. I attempted to define the object as follows:
function get_employee()
{
var employee = [];
var employee_data = '[{"id":"12","created_at":"2015-10-30T21:30:44.457Z",
"card":[{"id":"67","number":"89990","expires":"2018"}],
"verification":"required"}]';
employee = JSON.parse(JSON.stringify(employee_data));
return employee;
}
The intention is to access this structure within the $.ajax call:
$.ajax {
type: "POST",
url: 'my_url/process',
//other parameters
success: function(data){
//invoking the get_employee function
params = get_employee();
//attempting to utilize the returned object in JSON format
frm = $("<form action='/save/employee/database' method='POST'></form>");
frm.append("<input type='hidden' name='employee_id' value='" + params.employee.id + "'>");
frm.append("<input type='hidden' name='card_id' value='" + params.card.id+ "'>");
frm.append("<input type='hidden' name='request_id' value='" + data + "'>");
frm.submit();
}
}
However, when trying to log `params.token.id`, an error stating that `params.token.id` is undefined occurs, which also appears in the form. My query is: what could be causing this issue? What aspect am I overlooking?
NOTE: Whilst logging `params`, it displays the plain string from the function definition rather than [object object]. Debugging in firebug shows the response as an HTML string instead of JSON. The main concern is receiving it as JSON while being able to access its properties like a regular JavaScript object.
Your assistance in this matter is greatly appreciated!