This error message is indicating that there is a SyntaxError with an unexpected token 'o' when trying to

Query from AngularJS beginner:

I'm new to AngularJS and I'm attempting to utilize an asmx web service to display a grid. I have tested the web service and it correctly outputs the JSON data. Below is my controller code:

app.controller('SetupController', ['$scope', '$http', function ($scope, $http) {

    var url = 'app/pricefilessetup/grid.asmx/getGridJson';

    $http.get(url).success(function (data) {
        var myjson = JSON.parse(data);
        $scope.products= JSON.parse(myjson);
    });
}]);

Unfortunately, I am unable to paste HTML code here due to some restrictions, but it includes ng-controller directive and ng-repeat loop for parsing through the JSON data.

Upon running the application, I encounter the following error:

SyntaxError: Unexpected token o at Object.parse (native) and it points to the line below:

  $scope.questions = JSON.parse(myjson);

I attempted to check the value of myjson using alert and it displays [object Object], [object Object], ...

Is there something that I might be overlooking?

Answer №1

It appears that the data returned is already in JSON format, so there is no need for using JSON.parse(), unless it is stored as a string.

The $scope.products variable is assigned the value of the data object.

Answer №2

Could you please explain the reason for using JSON.parse twice in your code?

 var myjson = JSON.parse(data);
  $scope.products = JSON.parse(myjson);

You have already parsed the data object once, so why parse it again?

Additionally, if your data is returning a JSON result, there may not be a need to parse the object.

Instead, you can simply do this:

$scope.products = data;

Answer №3

Your myjson variable is already formatted as a JavaScript Object. No need to utilize JSON.parse on it.

Answer №4

An easy fix is to utilize the absolute URL:

var link = 'http://demo/app/pricefilessetup/grid.asmx/getGridJson';

Avoid using

var link = 'app/pricefilessetup/grid.asmx/getGridJson';
I have verified this method.

Answer №5

One issue I encountered was passing a string literal as a parameter to the JSON.parse() function.

For instance, calling JSON.parse('asdf') would result in the following error:

Uncaught SyntaxError: Unexpected token a
.

In a specific scenario within a Single Page Application built with Angular, I was passing an access token to the JSON.parse() function. Clearing the cookies in the browser resolved the problem for me.

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

Access several different dynamic URLs through a single Ajax request

I have a scenario where I am showing multiple dynamic links within the same div. The first link works perfectly fine with ajax loading the content, but the subsequent links do not work. How can I make it so that the content of all links can be loaded withi ...

Numerous asynchronous requests running simultaneously

After successfully querying a database using js/ajax for a single drop-down, I am now looking to enhance my solution by adding multiple identical drop-downs. The goal is to retrieve the same information when an option is selected without allowing duplicate ...

The feature to disable swiping with SwipeEnabled set to false seems to be

I am encountering an issue with react-navigation 5 in my react-native project. I am trying to disable drawer swipes while still allowing the user to close the drawer by clicking outside of it. I have researched this property in the react-navigation documen ...

Converting dynamic content within a div into an interactive link

I am currently working with Longtail's JW Player and facing some difficulties with a basic function. Since I am not familiar with the programming language terminologies, I will describe the issue step by step: There is a JavaScript code that displays ...

"Implementing NightwatchJS with parallel mode using Selenium Hub and Docker Compose

I have been experimenting with running tests in parallel using nightwatchjs within Docker using Selenium Hub. Initially, I managed to run the tests in parallel in Docker without Selenium Hub, but encountered issues with child processes timing out and requi ...

I am having trouble importing and receiving an error when trying to decode JSON to a string

I am currently using Netbeans to create a program that decodes JSON data into Strings. In order to accomplish this, I have included the json-simple-1.1.1 and json-20131018 JAR files as dependencies. Below is my code: import java.io.*; import org.json.si ...

Utilize AngularJS to Capitalize the First Letter and the Letter Following a Dot (.)

I am seeking a solution to capitalize the first letter in a given string, like so: sumo => Sumo Additionally, I need to capitalize strings in the following format: Dr.ravi kumar => Dr.Ravi kumar Although I have implemented an Angular filter that ...

Unable to delete with Laravel 5 using ajax

Currently, I am working on a project that involves the deletion of users from the users table using ajax. Despite my efforts to find solutions, nothing seems to be working. The error message I keep encountering is as follows: Failed to load resource ...

Sorry, but you can only use one 'in' filter in your query

this.ref.collection("users", ref => ref.where("uid1","in", [reciverId, senderId]) .where("uid2","in", [reciverId, senderId])) throws an error stating: "Invalid query. Multiple 'in' filters cannot be used." ...

Guide to custom sorting and sub-sorting in AngularJS

If I have an array of objects like this: [ { name: 'test1', status: 'pending', date: 'Jan 17 2017 21:00:23' }, { name: 'test2', sta ...

Styling the output of JSON content

I am currently working on a USSD application that requires JSON response in a specific format. The API I am using accepts responses like this: { "ispin": 1, "msisdn": "123456789", "partnerid": 10, "ShowInputBox": true, "displaystring": "content ...

Assess parameter within the constructor of the class

After instantiating an object with another object as a parameter, I have two goals: Firstly, I want to loop through the properties of the parameter and add them to the instance only if it matches exactly the type declared in the constructor. Is there a mo ...

Utilizing jQuery to Convert JSON Array Data into a Table

I am having trouble populating a table with data from a JSON array. The data is showing up in the console but I can't seem to get it into the table. Data is visible in the console screenshot below https://i.sstatic.net/C7L7d.jpg Here's what I& ...

Angular.js: The Art of Injecting JavaScript Dependencies

I came across information on DI and DI in Angular.js. My understanding is that DI in Angular.js allows controllers, factories, services, and other components to specify dependencies without having to create those dependencies. Some questions arise: At ...

JavaScript code to perform various functions

Suppose I am implementing the following JS Code: <script language="javascript" type="text/javascript"> function toggleVisibility() { var element = document.getElementById("elementId"); if(element.style.visibility.toLowerCase()== ...

Install only the necessary components from React Material UI

Is it possible to selectively install specific components from React Material UI? Specifically interested in the Autocomplete component, which can be found at this link. ...

Continuing to receive a JSONException error despite the JSON response being correct, an "<!DOCTYPE" type error is still

Recently, I have been learning from a tutorial on Androidhive about connecting Android with PHP and MySQL. However, when I attempted to implement the code, I encountered some issues with JSON response errors. After making modifications to the PHP files, I ...

jQuery remove highlighting only if it is currently applied

I am in need of a simple solution to remove the selected class when clicking on an already highlighted button. I want only one button to be highlighted at a time. http://jsfiddle.net/7wy7sjm5/1/ The reason why I first remove the class for all buttons is ...

What is the best way to manage executing functions on data that could potentially be undefined?

When working with React, I often encounter the need to write functions that rely on a component's state. One common issue I face is the necessity to check if a piece of state is defined before proceeding with any actions. For instance, I have a funct ...

The search box in Datatables is experiencing a glitch where it does not register any

Snippet to initialize datatable: jQuery('#table1').dataTable({ "scrollY": "200px", "scrollCollapse": true, "paging": false, "aaSorting": [], "oSearch": { "bSmart": false } }); The filter works perfectly when the table is first i ...