JSON Eval function encountered an identifier that was not anticipated

Here is a code snippet that I found for rating a page. It works perfectly on my local machine, but once I upload it to the server, it throws a syntax error.


function rtgAjax(elm, ratev) {
    var cerere_http = get_XmlHttp(); // get XMLHttpRequest object

     // define data to be sent via POST to PHP (Array with name=value pairs)
    var datasend = Array();
    for (var i = 0; i < elm.length; i++) datasend[i] = 'elm[]=' + elm[i];
    datasend = datasend.join('&') + '&rate=' + ratev;

    cerere_http.open("POST", 'ratingfiles/ratings.php', true);
    cerere_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    cerere_http.send(datasend);

    cerere_http.onreadystatechange = function() {
        if (cerere_http.readyState == 4) {
            eval(' var jsonitems = ' + cerere_http.responseText);

            if (jsonitems) {
                for (var rtgitem in jsonitems) {
                    var renot = jsonitems[rtgitem][2];

                    if (renot == 3) {
                        alert("You already voted \n You can rate again tomorrow");
                        window.location.reload(true);
                    } else addRtgData(rtgitem, jsonitems[rtgitem][0], jsonitems[rtgitem][1], renot);
                }
            }
        }
    }
}

This particular line of code,


eval(' var jsonitems = ' + cerere_http.responseText);

is causing a Syntax Error: Unexpected Identifier

Answer №1

Avoid using eval in this manner. A better approach is to utilize the following:

let jsonData;
try {
    jsonData = JSON.parse(httpRequest.responseText);
}
catch(error) {alert("Error parsing JSON data: "+error);}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for formatting input boxes on the client side

Q: How can I format my textbox so that when a user enters a number, such as one, it will be displayed as 0000001? The goal is to have any number entered be shown in 7-digit format. ...

Guide on how to iterate through the list of users within the component

Hello, I'm fairly new to working with react and I've encountered a challenge regarding implementing a loop within this component to display a list of all users. Despite trying numerous approaches, I haven't been able to figure it out. colum ...

Angular JS Sorting Wordpress Plugin allows users to easily organize and sort content

Seeking some assistance here, any help would be greatly appreciated. Currently using a Wordpress Angular JS plugin that is causing some unusual alphabetical sorting. This snippet of code showcases the taxonomy: <!-- for taxonomy --> <div ng-if ...

Chrome web client facing issue with Spring MVC REST api not consuming json data

My web application is built using AngularJS and Spring. It returns a JSON object via the following URL: http://localhost:8080/AngularJSPostFormSpringMVC/ https://i.sstatic.net/o8aeu.png The link above displays the output of the JSON data. However, when ...

How do I combine Firefox binary specification with adding the Firebug extension when using Selenium?

Presently I am utilizing the code below. var co = require('co'); var WebDriver = require('selenium-webdriver'); var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer; co(function *() { // async var ser ...

Looping through an array of data retrieved from the MS Graph API

When using node to call the MS Graph API with v1.0/users, the returned data shows a list of users in the following format after doing a console.log(users): { [ { displayName: "bob dole" }, { displayName: "steve st ...

Invoking a Jquery Dialog

Every time I click on the button to display a message, I encounter an issue where one MessageBox appears as expected. However, when I open the Jquery Dialog for the second time, two MessageBoxes pop up and the same problem occurs with three MessageBoxes on ...

Absolute positioned content causing unexpected spacing on top of relative positioned content

I've been experimenting with a fantastic technique by Chris Coyier for implementing full page video backgrounds with content scrolling over the video. However, I've encountered an issue where there's an unexpected gap to the right in Windows ...

including identical item in the array

<!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <link rel="stylesheet" href="http://www.w3schools.com/lib/w3.css"> <body> <script> var app = angul ...

Retrieve the specific array element from parsing JSON that includes a particular phrase

I need help filtering array values that contain the phrase 'Corp' Currently, all values are being returned, but I only want the ones with "Corp" var url = "/iaas/api/image-profiles"; System.debug("getImageProfiles url: "+url ...

Is there a way to programmatically parse JSON Data on an iPhone device?

I attempted to decode the JSON Data provided, but encountered difficulties in parsing it correctly. If anyone has insight on this matter, please share your knowledge. Below are snippets of my code and the JSON data: NSString *responseString = [[NSString ...

Establish a variable in XSL to define the tabIndex

My XSL code has been designed to read an XML file and generate input elements of type text for each child node. The XML file structure is as follows: For node c, two input boxes are created in the format: Label(com 1) :input box--------------------- Label ...

A dynamic Angular search box designed for filtering columns in a table

I have a functioning table code that displays data related to domains: Now, I would like to enhance this table by adding a dynamic search box specifically for filtering the column named domain. As the user types in new characters in the search box, the ta ...

Tips on capturing a URL using JQuery

Currently, I am in the process of importing an external HTML file into my webpage. Within this file, there is a JavaScript method linked to a form submission event. function ValidateInput(){ //some code if(validationFailed){ ...

Error encountered while trying to access `cookies.js` file in the `axios` module located at `../node_modules/axios/lib/helpers/`. The specific

Every time I attempt to retrieve data using axios.get(url), an error is thrown. Despite trying numerous solutions, nothing seems to work and this issue has persisted for quite some time. Any assistance that can be provided would be greatly appreciated. Er ...

The Express router is failing to recognize the mongoose model

Currently, I am developing a node.js application and encountering an issue where I receive a reference error stating that Post is not defined every time I run this specific code. Interestingly, when I move the post route from submit.js to app.js, the code ...

Shifting divs to different positions upon clicking

I am currently working on a project where I have three divs in a container positioned next to each other. My goal is to make them change their positions when clicked. For example, clicking on the left div should move it to the center position. Here is my p ...

Splitting data in NodeJS sockets

Whenever I encounter the need to divide data, my standard practice involves converting it into a string format. Here's an example of how I handle data in my function: socket.on('data', function (data) { var str = data.toString().spl ...

What steps can be taken to send the user to the login page after their session token has expired

Currently, I am using a Marionette + Node application. I have noticed that when the token expires, the application does not respond and the user is not redirected to the LogIn page. My question is, how can I set up a listener to check the session token s ...

The dropdown menu disappears when I hover over the tab, making it impossible for me to navigate it in the react app

My navigation component includes a Games tab with a dropdown menu that appears when you hover over it. However, I'm facing an issue where the dropdown menu closes before the user can transition from hovering over the games tab to the actual dropdown m ...