0

I am using Json in javascript. I'm reading the json from test.js. How can I read for(;;).

 <script type="text/javascript">
    $.getScript('ajax/test.js', function(data, textStatus){
        jQuery.each(data, function(return) {
            alert(return.name);
            alert(return.surname)
        });
    });
    </script>

-

TEST JS

for (;;);
{
 "name"     : "Mustafa Can",
 "surname"  : "Pala"
};
5
  • Can you please clear this up? What does How can I read for(;;) mean? Commented Sep 26, 2011 at 2:33
  • 1
    Change test.js, because that's just not valid JSON. Commented Sep 26, 2011 at 2:36
  • @Matt: It's an anti-XSS technique Commented Sep 26, 2011 at 2:46
  • 1
    @SLaks really? [citation-needed] Commented Sep 26, 2011 at 3:12
  • @Matt: It prevents attackers from including the JSON in a script tag after defining property setters so they can intercept the data. Commented Sep 26, 2011 at 11:26

1 Answer 1

2

You need to call $.get to fetch the content of the script as a string, then remove the for(;;);:

$.get('ajax/test.js', function(data, textStatus){
    var json = data.replace(/^for\(;;\);/, "");
    var obj = $.parseJSON(json);

    alert(obj.name);
    alert(obj.surname)
}, 'text');
Sign up to request clarification or add additional context in comments.

1 Comment

@Gürkan: To prevent jQuery from parsing it as JSON (which would fail) or executing it as Javsacript (which would freeze)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.