1

I am calling a PHP file to get the requested XML file and storing its data to a JavaScript variable. Here is the code:

var albumXML = null;
<?php $fname = "folder/" . $_GET['getFile'];
    $contents = file_get_contents($fname);
    echo 'albumXML = "' . $contents . '";';
?>

But the console shows the error: Uncaught SyntaxError: Unexpected token <

I think it's the starting of the $contents's first tag start. How can I store that data?

2
  • do you have CRLF in the $contents variable?. Even if you have an error doing right-click and checking source code helps a ton sometimes to detect strange behaviors Commented Nov 22, 2012 at 12:20
  • this php file loaded into the dom via ajax. So the javascript in this file I can't find anywhere ! not in the source code! Commented Nov 22, 2012 at 12:25

1 Answer 1

1
<?php
    $fname = "folder/" . $_GET['getFile'];
    $contents = file_get_contents($fname);
?>
var albumXML = '<?php echo addslashes(preg_replace('@(\n|\r|\r\n)+@', ' ', $contents)); ?>';
Sign up to request clarification or add additional context in comments.

12 Comments

same Error ! Uncaught SyntaxError: Unexpected token <
Try now. I added a preg_replace to substitute any newlines by spaces.
still the same error.! I was before this using the ajax request to get that data and was working fine. But when there is huge xml files. It stops working maybe due to server restrictions. So i tried this direct method but still not working
@user1820644 Are you using single or double quotes for attributes in your XML?
there are no attributes in the XML. All simple tag without any attribute.
|

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.