Why I receive different errors on real Android device and Chrome - json

I develop a mobile app in phonegap. When I test it on Chrome I receive no errors and the database if filled correctly. When I try it on a real device through eclipse i receive the below errors. Why I am getting these errors?
09-22 19:56:49.568: E/Web Console(6912): Uncaught SyntaxError: Unexpected token } at file:///android_asset/www/index.html#page2:1
09-22 19:56:49.558: E/Web Console(6912): Uncaught SyntaxError: Unexpected token ILLEGAL at file:///android_asset/www/index.html#page2:1
and where to find } because in index.html there is no such a char.
I receive these error in my try to read some JSON.
PS I use this code to read some json
function billpaymentstxt()
{
var billpaymentsjson = '{"posts" : [',
i,
line = 0,
billpayments = 0,
mybillpaymentsjson,
Amountint,
Amountdec;
jQuery.ajaxSetup({
'beforeSend' : function(xhr) {
xhr.overrideMimeType('text/plain; charset=iso-8859-7');
},
});
jQuery.get('BillPayments.txt',function(data){
// alert(data.length);
line=0;
for (i = 1; i <= ((data.length)/20); i += 1) {
billpaymentsjson += '{"Id" :' + '"' + data.substr((line+0), 10).trim() + '"' + ',';
Amountint = data.substr((line+10), 7).trim();
Amountdec = data.substr((line+17), 2).trim();
billpaymentsjson += '"PreviousPayments" : ' + '"' + Amountint + '.' + Amountdec + '"' + '}';
line = i * 20;
//alert(line);
if (line == data.length)
{
billpaymentsjson += ']';
}
else
{
billpaymentsjson += ',';
}
}
if (line == 0)
{
billpaymentjson += ']';
}
billpaymentsjson += "}";
//alert(billpaymentsjson);
mybillpaymentsjson = jQuery.parseJSON( billpaymentsjson );
if (((mybillpaymentsjson.posts.length)) == 0) {
$('#mycontent article').html('<strong>bill - <font color=blue>OK</font></strong>');
}
for (i = 0; i < (mybillpaymentsjson.posts.length); i += 1) {
notesdb.transaction((function(i) {
return function(t){
t.executeSql('INSERT into billpayments (barcode, amount, receiptno) VALUES (?,?,?);',
[mybillpaymentsjson.posts[i].Id, mybillpaymentsjson.posts[i].PreviousPayments, 0],
function(){
billpayments = billpayments + 1;
$('#mycontent article2').html(billpayments + '/' + mybillpaymentsjson.posts.length + ' <strong>billpayments</strong>');
if ((mybillpaymentsjson.posts.length) - billpayments == 0) {
$('#mycontent article2').html('<strong>billpayments - <font color=blue>OK</font></strong>');
}
}
);
};
})(i));
}
});
}

From what I know JSON cannot handle ISO-8859-7.
http://www.php.net/manual/en/function.json-encode.php
"This function only works with UTF-8 encoded data." Maybe you could try some conversion

Related

Card payment error 508 -Realex payments error number: 61,754

i am using realex payment with iframe
i can load the payment page correctly, but as soon as i hit 'Pay Now'
it return Error: 508
Message: An error has occurred processing your request. Please contact the merchant whose goods or services you are purchasing quoting the following error number: 61,754
(most of time it return correct response string either successful and declined) but sometimes it return above error code)
any idea what is mean and how to solve this issue?
<script type='text/javascript'>
function iRedirect(redirectUrl, arg, value) {
console.log(redirectUrl);
try {
var form = $('<form action="' + redirectUrl + '" method="post">' +
'<input type="hidden" name="' + arg + '" value="' + value + '"></input>' + '</form>');
$('body').append(form);
console.log(form);
$(form).submit();
}
catch (e) {
alert(e.message);
}
}
function displayMessage(evt)
{
var message;
try {
var iOrigin = '<%=ConfigurationManager.AppSettings["RealexResponseDomain"] %>';
if (evt.origin == iOrigin) {
message = evt.data.toString();
console.log(message);
if (message.indexOf("Error") == 0) {
var ErrorJsonStr = message.toString().split(":");
var ErrorJsonStr1 = ErrorJsonStr[1].split("<BR>");
var reDirectPath = "{\"" + ErrorJsonStr[0] + "\"" + ":" + "\"" + ErrorJsonStr1[0] + "\"" + "," + "\"" + ErrorJsonStr1[1] + "\"" + ":" + "\"" + ErrorJsonStr[2] + "\"" + "}";
iRedirect("Response.aspx", "JsonStr", encodeURIComponent(reDirectPath));
}
else {
if (isJson(message) == true) {
var message1 = JSON.parse(message);
//alert(message1);
console.log(message1);
if (message1.hasOwnProperty('pas_uuid')) {
iRedirect("Response.aspx", "JsonStr", encodeURIComponent(message.toString()));
}
else {
//check if this transaction is already exist
//do redirect
//alert("not pas_uuid" + message1);
console.log("not pas_uuid" + message1);
}
}
}
//get message and check result
}
else {
console.log("not data");
}
}
catch (err) {
console.log(err.message);
}
}
function isJson(str) {
try {
JSON.parse(str);
}
catch (e)
{
console.log(e.message);
return false;
}
return true;
}
if (window.addEventListener) {
// For standards-compliant web browsers
window.addEventListener("message", displayMessage, false);
}
else {
window.attachEvent("onmessage", displayMessage);
}
</script>
There are two major scenarios where this message may be displayed on the HPP.
There was a temporary issue with the HPP Sandbox environment.
The Merchant ID and Account you are using has 3D Secure 1 enabled, but you used a non-3D Secure enabled test card.
If you think it wasn't either of these issues, please provide our Support Team with an example Order ID of a transaction where this occurred and they will be able to look at the logs in more detail.

Sort JSON data by Date/Time value

Hope someone could help with this small task. I have an array of text blocks that have a DateTime value assigned to them. I would like to publish those text blocks sorted by DateTime so that the latest updated item is always on top.
Here is the script:
function jsonCallBack(data) {
var strRows = "";
$.each(data.News, function(i, item) {
var htmlNewsBody = item["htmlNewsBody"];
var maxLength = 120
var trimmedString = htmlNewsBody.substr(0, maxLength);
trimmedString = trimmedString.substr( 0, Math.min( trimmedString.length,
trimmedString.lastIndexOf(" ") ) );
strRows += "<div id='nrNewsItem-" + i + "'>";
strRows += "<h3>" + item["txtTitle"] + "</h3>";
strRows += "<p>" + item["dtDateTime"] + "</p>";
strRows += "<p>" + trimmedString + "...</p>";
strRows += "</div>"
});
$("#printHere").html(strRows);
};
Also have a working jsFiddle with JSON data.
You can add a custom compare method:
function compare(a,b) {
if (a.dtDateTime < b.dtDateTime) {
return 1;
}
if (a.dtDateTime > b.dtDateTime) {
return -1;
}
return 0;
}
Then in your function:
function jsonCallBack(data) {
data.News.sort(compare);
....

nodejs json.stringify a 1gb object running out of memory

I'm trying to json.stringify a 1 gb object so that I can write it to disk. I get FATAL ERROR: JS Allocation failed - process out of memory
What can I do to stringify successfully?
You can stringify bit by bit manually: if y is a key of x, then JSON.stringify(y) + ":" + JSON.stringify(x[y]) gives you one segment.
Using fs.appendFileSync, for example, you can use:
var output = "out.json";
fs.writeFileSync(output, "{");
var first = true;
for(var y in x) {
if(x.hasOwnProperty(y)) {
if(first) first = false;
else fs.appendFileSync(output, ",");
fs.appendFileSync(output, JSON.stringify(y) + ":" + JSON.stringify(x[y]))
}
}
fs.appendFileSync(output, "}");
You can also use a Write Stream
Extended with Object, Array checker
var first, y, i$, ref$, len$, toString$ = {}.toString;
switch (toString$.call(x).slice(8, -1)) {
case 'Object':
fs.writeFileSync(output, '{');
first = true;
for (y in x) {
if (x.hasOwnProperty(y)) {
if (first) {
first = false;
} else {
fs.appendFileSync(output, ',');
}
fs.appendFileSync(output, JSON.stringify(y) + ':' + JSON.stringify(x[y]));
}
}
fs.appendFileSync(output, '}');
break;
case 'Array':
fs.writeFileSync(output, '[');
first = true;
for (i$ = 0, len$ = (ref$ = x).length; i$ < len$; ++i$) {
y = ref$[i$];
if (first) {
first = false;
} else {
fs.appendFileSync(output, ',');
}
fs.appendFileSync(output, JSON.stringify(y));
}
fs.appendFileSync(output, ']');
break;
default:
fs.writeFileSync(output, JSON.stringify(x));
}

JsTree with JSON stuck on loading

I am using JSON to display a JsTree. The JSON is being built up as a string via a recursive function. Now I went through a few tests with smaller/less complicated trees and got it to work. I used JSONLint to check for valid JSON and eventually got the correct syntax. Now when I try and display the intended big tree its just stuck with the loading .gif (which used to be because the JSON was incorrect) but after checking it on JSONLint it was correct.
Any possible causes for this? I doubt the tree could be too big or anything.
Recursive Function:
public void getViewTree(ref string tree, Int32? id = null)
{
var topNodes = (from items in db.AssessmentViewItems
select items).Take(1);
#region getChildren via LINQ
if (id == null)
{
topNodes = from items in db.AssessmentViewItems
where items.ParentAssessmentViewItemID == null
&& items.AssessmentViewID == 17
select items;
}
else
{
topNodes = from items in db.AssessmentViewItems
where items.ParentAssessmentViewItemID == id
&& items.AssessmentViewID == 17
select items;
}
#endregion
int counter = 1;
int max = (int)topNodes.Count();
foreach (var node in topNodes)
{
if (node.ParentAssessmentViewItemID == null)
{
{\"id\":\"532topNode\",\"selected\":true},\"children\":[null,
tree += "{\"data\":\"" + node.Title.Trim().Replace("\"","").Replace("("," ").Replace(":"," ").Replace("-"," ").Replace("&","and").Replace("/"," ").Replace("\\"," ").Replace(","," ").Replace("•", " ") + "\",\"attr\":{\"id\":\"" + node.AssessmentViewItemID + "\", \"selected\":true}, \"children\":[";
getViewTree(ref tree, node.AssessmentViewItemID);
tree += "}]";
if (counter < max)
{
tree += "},";
}
}
else if (node.Type.Equals("Legal Topic"))
{
tree += "{\"data\":\"" + node.Title.Trim().Replace("\"", "").Replace("(", " ").Replace(":", " ").Replace("-", " ").Replace("&", "and").Replace("/", " ").Replace("\\", " ").Replace(",", " ").Replace("•", " ") + "\",\"attr\":{\"id\":\"" + node.AssessmentViewItemID + "\", \"selected\":true}";
if (counter < max)
{
tree += "},";
}
}
else
{
var topNodes1 = from items in db.AssessmentViewItems
where items.ParentAssessmentViewItemID == node.AssessmentViewItemID
&& items.AssessmentViewID == 17
select items;
if (topNodes1.Count() > 0)
{
tree += "{\"data\":\"" + node.Title.Trim().Replace("\"", "").Replace("(", " ").Replace(":", " ").Replace("-", " ").Replace("&", "and").Replace("/", " ").Replace("\\", " ").Replace(",", " ").Replace("•", " ") + "\",\"attr\":{\"id\":\"" + node.AssessmentViewItemID + "\", \"selected\":true}, \"children\":[";
}
else
{
tree += "{\"data\":\"" + node.Title.Trim().Replace("\"", "").Replace("(", " ").Replace(":", " ").Replace("-", " ").Replace("&", "and").Replace("/", " ").Replace("\\", " ").Replace(",", " ").Replace("•", " ") + "\",\"attr\":{\"id\":\"" + node.AssessmentViewItemID + "\", \"selected\":true}";
}
getViewTree(ref tree, node.AssessmentViewItemID);
if (topNodes1.Count() > 0)
{
tree += "}]";
}
if (counter < max)
{
tree += "}";
tree += ",";
}
}
counter++;
}
}
JS:
$(function () {
$("#demoTree").jstree({
"json_data": {
"data": treeModel
},
"plugins": ["themes", "json_data", "ui"],
});
});
Calling Recursive Function:
string tree = "[";
getViewTree(ref tree);
tree += "}]";
return View("About", "_Layout", tree);
After using Chrome Dev Tools, the error that I get from it:
Uncaught SyntaxError: Unexpected token ILLEGAL (program):54
Uncaught Neither data nor ajax settings supplied.
I did check it for syntax on JSONLint. Small tree is generated fine without those 2 errors
Problem got solved by using DevExpress tree. Exact same JSON string.

AS: if statement evaluating incorrectly

I have a very simple piece of logic as follows:
var divert:Number = 0;
for (var connection in _connections) {
trace("target: " + _connections[connection].target + " || i: " + (i + 1));
if(int(_connections[connection].target) != (i + 1)) {
trace("bad connection");
divert++;
}
}
The problem is that when i + 1 and int(_connections[connection].target) are equal the if statement is returning true as can be seen in the output of my trace() statements below:
target: 0 || i: 1
bad connection
target: 1 || i: 1
bad connection
Can anyone see what could be causing this to happen?
EDIT: The function this is contained in as per request:
public function loadListener(i:Number, onProgress:Function, onComplete:Function):Void
{
trace("load listening to: "+i);
trace("next in queue: " + _queues["lower"][0] + " | " + _queues["upper"][0]);
_functions[i] = {onProgress:onProgress, onComplete:onComplete};
if (_queues["lower"][0] != i + 1 || _queues["upper"][0] != i + 1) {
var divert:Number = 0;
for (var connection in _connections) {
trace("target: "+_connections[connection].target+" || i: "+(i+1));
if(int(_connections[connection].target) != (i + 1)) {
trace("bad connection");
divert++;
}
}
if (divert == _connections.length) {
_diversion = i + 1;
trace("divert: "+divert+" || connections: "+_connections.length);
}
}
}
First of all, why use a for(var) loop when you can use
var divert:Number = 0;
for each(var connection in _connections) {
trace("target: " + connection.target + " || i: " + (i + 1));
if(int(connection.target) != (i + 1)) {
trace("bad connection");
divert++;
}
}
To debug further, replace
trace("target: " + connection.target + " || i: " + (i + 1));
with
trace("target: " + int(connection.target) + " || i: " + (i + 1));
If this traces zero, you know where the issue is.
You could try doing
if(connection.target.toString() != (i + 1).toString()) {