I've been using the following function for some time with great success, but just recently it stopped working. I've not changed anything; same browser, server, same jquery library, everything's the same so I'm lost on what's wrong. The following function takes a long string and converts it to an object:
$(function()
{
var _sid = getUrlVar('AICC_SID');
var _url = getUrlVar('AICC_URL');
$.get(_url,{command:"GetParam",version:"2.2",session_id:_sid},function(response)
{
var _obj = toJSON(response);
console.log(_obj);
});
});
function toJSON(str)
{
var obj = {};
str.replace(/([^=]+)=(.*)\n/g, function (_, name, value)
{
console.log('name : ' + name);
console.log('value : ' + value);
obj[name] = $.trim(value);
});
return obj;
}
Here's the string that get's converted:
ERROR=0
ERROR_TEXT=Successful
VERSION=2.2
AICC_DATA=[CORE]
STUDENT_ID=0425655
STUDENT_NAME=Doe, John B
SCORE=
TIME=00:00:00
CREDIT=C
LESSON_LOCATION=1_5
LESSON_STATUS=NULL
[Core_Lesson]
[Objectives_Status]
The problem is the .replace()
appears to not be running. The toJSON()
function converts the string to an object, but it always returns and empty object. Hopefully that clears up what's wrong.