admin管理员组

文章数量:1332389

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: .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.

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/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.

Share edited Feb 20, 2013 at 20:15 Robert Harvey 181k48 gold badges348 silver badges513 bronze badges asked Jan 8, 2012 at 21:35 Jacob BrunsonJacob Brunson 1,4924 gold badges24 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You have invalid javascript returned from fetchData.php which is why the eval method 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 the addslashes function which is causing the damage. So maybe you could try something along the lines of:

echo($response["success"].";"); 

本文标签: xmlhttprequestJavascript Unexpected token ILLEGALStack Overflow