I have a PHP script (fetchData.php) that fetches some data and outputs it to a page.
<?php
require 'config.php';
require 'jsonapiSDK.php';
$api = new JSONAPI($ip_address, $jsonapi_port, $username, $password, $salt);
$response = $api->call('BWMFunction');
echo(addslashes($response["success"].";"));
?>
You can see the output here: http://justicecraft.net/worldmap/fetchData.php
I have another page that uses an XMLHttpRquest to get the response from fetchData.php
Here’s the JavaScript for it. It’s supposed to take the response, and eval() it (to create an array called BWMFunction) then pass that array to another function I have. The Illegal token error occurs when I try to eval() the response.
function fetchData() {
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
res = xmlhttp.responseText;
alert(res);
eval(res);
generate(BWMFunction);
}
}
xmlhttp.open("GET", "fetchData.php", true);
xmlhttp.send();
}
This is my first time on StackOverflow, so any help would be appreciated. I have googled around for quite a while now, but none of the answers helped me.
You have invalid javascript returned from
fetchData.phpwhich is why theevalmethod crashes. Instead of\"in the output you should have simple". I know strictly nothing about PHP but I am ready to bet some bucks that its theaddslashesfunction which is causing the damage. So maybe you could try something along the lines of: