/    Sign up×
Community /Pin to ProfileBookmark

eval – json – too many characters

I am generating a json structured array with php. Then, I use the XMLHttpRequest object to call in the string. Then when the string in loaded in , I eval it, so that I can parse through it with javascript. Problem here is that when I eval the string, it cuts off everything after 254 characters. I did some research on this, and it states some bugs on this for netscape 2.0. I am using netscape 8 for testing. Does anyone know of a workaround? or any other solution?

[CODE]var json = eval(‘(‘+this.req.responseText+’)’);[/CODE]

‘this.req.responseText’ being my string generated from PHP. If i dont eval it, I get all my of my string. But, I have to eval it to be able to parse it in js.

Thanks
-Trey

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceMay 16.2006 — What does the content of the string look like?
Copy linkTweet thisAlerts:
@UltimaterMay 16.2006 — Eval should be avoided. I'd recommand using the Function constructor.

e.g.
<i>
</i>&lt;script type="text/javascript"&gt;
var str="globalVarable="hi there!";";
var tmpFunc=Function(str);
tmpFunc();
alert(globalVarable)
&lt;/script&gt;

simply replace [color=blue]str[/color] with [color=blue]this.req.responseText[/color] and adjust the contents to be treated as the body of the anonymous function then there should be no issues.

FYI the following three lines are interchangable:
<i>
</i>function alertMe(a){alert(a)};alertMe("hi")
alertMe=function(a){alert(a)};alertMe("hi")
alertMe=Function("a","alert(a)");alertMe("hi")

as well as these three:
<i>
</i>function alertMe(){alert("hi")};alertMe()
alertMe=function(){alert("hi")};alertMe()
alertMe=Function("alert("hi")");alertMe()
Copy linkTweet thisAlerts:
@BassMasterFlashauthorMay 16.2006 — Problem - Eval would only parse 254 characters. The existing code was:

[CODE]var json = eval('('+this.req.responseText+')');[/CODE]

The Fix:

[CODE]var json=new Function("return " + this.req.responseText)();[/CODE]


Thanks Ultimater for your help.

-Trey
×

Success!

Help @BassMasterFlash spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...