0

http://localhost/project1/index.php

//AJAX region
$(function(){
	$.ajax({
		type : 'GET',
		url : 'https://jsonplaceholder.typicode.com/posts/1',
		success : function(data){
			console.log('success \n', data);
		},
		error : function(data){
			console.log('error', data);
		}
	});
});//AJAX region
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

it's easily loading data from http://localhost/project1/json.php
console: enter image description here

data is string, if I use $.parseJSON(data) or JSON.parse(data) i get following error... enter image description here enter image description here

I want data as realtime JSON object, so that I can access each properties & values.

7
  • Can you add the actual JSON to the post rather than a screenshot. That being said the screenshot doesn't appear to be valid JSON. Commented Feb 22, 2018 at 15:35
  • The "objects" within state 1, 2 ... n are invalids. Probably the backend should return as arrays rather than objects. Commented Feb 22, 2018 at 15:36
  • Your json.php returns invalid JSON, how are you generating it? You should simply use json_encode PHP function and if you want to do it right before echoing it set the header to JSON with header('Content-type: application/json');. Commented Feb 22, 2018 at 15:37
  • There are multiple problems with the JSON. All property names should also be enclosed in double quotes ", for example Commented Feb 22, 2018 at 15:38
  • Please add code from region.php. The problem appears to be there. Commented Feb 22, 2018 at 15:39

2 Answers 2

2

Observation 1: unnecessary parseJSON

No need to call parseJSON. Use dataType property of $.ajax to load your data directly in JSON format:

$.ajax({
    type : 'GET',
    dataType: 'json', // here
    url : 'http://localhost/project1/region.php',
    success : function(data) {
        // data is already a JS object :)
        console.log('success \n', data);
    },
    error : function(data){
        console.log('error', data);
    }
});

Observation 2: wrong JSON

Code above will still throw the same error, because of

unexpected token ,

It refers to the trailing comma after all the last elements your object and its children. Remove that comma and there will be no more errors.

For example:

"State1" : {
    "City 1",
    "City 2",
    "City 3",
}

There should NOT be a comma after "City 3". The same goes for other states, and for whole "country2" object too.

There is another error: your States objects are arrays, but you are writing then as objects. Change curl brackets to square brackets for "State1" and "State2".

Sign up to request clarification or add additional context in comments.

Comments

1

The json object you are returning is invalid json, for the state objects, you need to make them arrays. so:

"State1":["City1","City2","City3"]

When the parser parses this object it expects a second value per city like "City1 ": "Second Value"...since there is no second value its breaking.

2 Comments

{ "country 1":{ "State 1" : ["City 1","City 2","City 3"], "State 2" : ["City 1","City 2","City 3"], }, "country 2":{ "State 1" : ["City 1","City 2","City 3"], "State 2" : ["City 1","City 2","City 3"], }, } but still error
], }, } This is the last part, delete those commas.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.