Hmm, JavaScript is throwing a JSON parse error related to single quotes. But where exactly is

When using an upload php script with AS3 and now with JavaScript, I encountered a test feature that should return this if everything is okay:

{"erro":"OK","msg":"","descr":"op:ini,urlArq:\/Library\/WebServer\/Documents\/www\/sintran\/fotos\/teste.log"}

However, the JavaScript code is throwing an error: JSON Parse error: Single quotes (') are not allowed in JSON

It's clear from the response that there are no single quotes present. It's possible that the "descr" property might be causing confusion due to certain characters like : and ,, but I prefer not to alter it so that AS3 can continue reading this property (since it contains useful data).

Any thoughts on how to resolve this issue?

Edit: The slashes are properly escaped with backslashes, although they are not displaying correctly above.

Edit: Here is the code snippet for reference:

formUpload.prototype.ini_test = function(evt){
    console.log("ini_test - Server response: "+evt.target.responseText);
    var jsonRes = evt.target.responseText;
    jsonRes = jsonRes.slice(jsonRes.indexOf("{"), jsonRes.lastIndexOf("}")+1);
    try{
        var Res = JSON.parse(jsonRes);
        if(Res.erro!="OK"){
            this.monitor_ini.innerHTML = "Error: "+Res.msg;
        } else {
            this.monitor_ini.innerHTML = "";
            var jExts = this.form.arquivo_tipos.value;
            this.Exts = JSON.parse(jExts);
            this.monitor_info.innerHTML = this.ajuda();
        }
    } catch(e){
        this.monitor_ini.innerHTML = "Error!";
        console.log("formUpload ini_test Error: "+e.message+" Server response: "+jsonRes);
        alert("Upload test error. JavaScript response: "+e.message);
    }
}

Edit: Logging all results to console log - however, this time I tried without escaping slashes, resulting in a different final string:

ini_test - Server response: {"erro":"OK","msg":"","descr":"op:ini,urlArq:\/Library\/WebServer\/Documents\/www\/sintran\/fotos\/teste.log"}
formUpload ini_test Error: JSON Parse error: Single quotes (') are not allowed in JSON Server response: {"erro":"OK","msg":"","descr":"op:ini,urlArq:/Library/WebServer/Documents/www/sintran/fotos/teste.log"}

Answer №1

It appears that the issue lies in the .responseText being valid JSON, which suggests that the error originates from this code snippet:

var jExtensions = this.form.file_types.value;
this.Extensions = JSON.parse(jExtensions);

The data fetched from the form input assigned to .value may not be in a proper JSON format, hence triggering the error.

Since both processes are within the same try/catch block, it's challenging to identify the exact source of the problem. Using separate try/catch blocks might offer more clarity in troubleshooting.

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

What is the best way to extract a single image from JSON data using AngularJS?

Using ng-repeat in HTML, how can I display only a single image instead of all images? <img class="size" ng-src="{{image.image}}" /> In CSS, I aim to define the size of the image. .size { margin-left:0.3em; width:11em; height:11em; border:0.4em sol ...

It appears that the JS function is not being triggered

I am facing an issue with my JavaScript code where a ball is not showing up even though it should. I have an array with only one element, and when that element matches with "F2", the ball is supposed to appear. I suspect there might be a problem with my us ...

What is the best approach to create a JavaScript search filter function spanning two pages?

Page1.html has a form with a drop-down menu containing options Color and Shape. There is also a submit button. Assuming Page2.html displays various shapes like Blue Square, Red Square, Blue Circle, Red Circle, is it feasible to create a JavaScript file th ...

Tips for implementing a search function with DynamoDB using the "contains" operator

I'm currently working on implementing a search function in my React application. Here is the structure of my DynamoDB table: --------------------- movie_id | movie_name --------------------- 1 | name a --------------------- 2 | name b ...

Utilize the API to seamlessly integrate a new contact into your Pushbullet account

Struggling to include a new contact in my Pushbullet contacts list through the API. I'm sending this json data in my POST request to { "name": "some name", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbda ...

What could be causing the discrepancy in the model value when using checkboxes in AngularJS?

Something strange is happening with my code. I have a checkbox in my view that has a directive to display its value when it changes. Surprisingly, it works fine in Firefox, showing the correct values. However, in other browsers like Chrome, it shows the op ...

Oops! An unexpected field was encountered while trying to use the uploadMultiple function from dropzone.js

I was looking for a way to allow users to select images to accompany their reviews. That's when I came across dropzone.js. However, I encountered an issue when trying to send multiple images in one request. I expected the req.files to contain an arra ...

What is the best way to insert an image in front of text within a table cell that can be identified by its class name?

JavaScript Question: function addFlags(){ while(i < $('.tabledata').length){ var current_val = $('.tabledata').eq(i).text(); arr.push(current_val); $('.tabledata').eq(i).html("<img s ...

Javascript problem: Trouble with event.clientX position

I found a great resource on learning javascript at this website, I'm having trouble understanding the code snippets in function XY(e, v), specifically these two lines: event.clientX + document.documentElement.scrollLeft and event.clientY + document ...

The XMLHttpRequest response states that the preflight request did not meet the access control check requirements

I am attempting to subscribe to a firebase cloud messaging topic by sending an http post request. var data = null; var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState ...

I am experiencing an issue with Array.some not functioning properly within my React component, whereas Array.map is working

I am attempting to utilize Array.prototype.some() within my React component to check if a particular value exists in my array of objects. However, I keep encountering the error message data.some(...) is not a function. Interestingly, Array.prototype.map() ...

Encountered an issue parsing the JSON file, possibly due to a concealed value within the JSON content

After coming across this JSON file online: https://drive.google.com/file/d/1zh_fJJNWs9GaPnlLZ459twSubsYkzMi5/view?usp=share_link Initially, it seemed fine and passed various online json schema validators. However, upon attempting to parse it locally usin ...

Steps to include a data-xx attribute in the displayed <table> within a Vuetify 2 v-simple-table

I am facing an issue where I want to include an HTML data-xxxx attribute to the HTML <table> within a v-simple-table. However, when I add the data attribute to the <v-simple-table>, it ends up being placed in a surrounding div two levels above ...

The function "xml find" does not exist

After successfully running the following ajax call, I encounter an issue: $.ajax({ url: "services/orders/<%=OrderServices.Action.BULK_SUPPLIER_DISCOUNT%>", data: params, complete: function(xhr) { i ...

Merge information from various sources using ajax

Currently, I have a single ajax request that retrieves data from an API and uses it to generate a table. Now, I'm looking to modify the code so that it can retrieve data from two different URLs and merge them into the same table (retTable). Below is ...

Is there a way to iterate through an array in reverse, starting from the last element and ending at the first element?

Is there a way in JavaScript to map an Array starting from the last index and going all the way to the beginning descending, without iterating from the beginning index? I'm looking for a more efficient method or feature I might have overlooked. Any s ...

What is a way to retain the value of a variable after a request, while starting off with a different value on the initial load?

In my Flask application, users have the option to choose a specific time period with a start date and an end date. When the page initially loads, I want the start date to default to the first day of the current month and the end date to be the current day. ...

Using async/await in React to retrieve data after a form submission

I am currently facing an issue with displaying data fetched from an API on the screen. My goal is to retrieve data when a user types something in a form and clicks the submit button. The error message I am encountering is: TypeError: Cannot read propert ...

Transform JSON data into an array using the nlohmann/json library

It's been more than two decades since I last delved into C++, so my skills are a bit rusty in this area. Currently, I am utilizing the nlohmann/json json.h library and facing the challenge of converting a JSON string that represents an array of objec ...

Two components, one scroll bar, both moving within the same plane

For those interested, here is the JSFiddle link for further exploration: https://jsfiddle.net/q6q499ew/ Currently, there is a basic functionality in place where when you scroll past a certain point, an element becomes stuck until you start scrolling back ...