AngularJS getting in trouble with my JSON - json

I have got a JSON object from my website:
{ "ID":"102”,
"role":{“subscriber”:true},
"first_name”:”Test 3”,
"last_name”:”Test 4”,
"custom_fields":{ “job_title”:”testing”},
}
and AngularJS to manage the dynamic content but it doesn't seem to be working:
var app = angular.module('myApp', []);
function PeopleCtrl($scope, $http) {
$scope.people = [];
$scope.loadPeople = function () {
var httpRequest = $http({
method: 'POST',
url: '/echo/json/',
data: mockDataForThisTest
}).success(function (data, status) {
$scope.people = data;
});
};
}
Here is a JSFiddle.
Can anybody help me with displaying data?

#qqruza to get the callback working properly in your jsfiddle.net/1zuteco7, change the url to this:
http://test.eventident.com/api/last_posts/siteid=&callpage=1&perpage=10&callback=JSON_CALLBACK
Notice the JSON_CALLBACK in the end. The rest of your app will still not work though cause you are not picking the right bindings from the returned data in your repeat directive. Try console.log(data) in the success function to click through the returned object and get to the right paths.

There were a number of issues with your JSON, I have resolved them.
It had different types of quotes in there. I have replaced them with ".
It now looks like this:
[{         
"ID": "100",
"role": {            
"subscriber": true         
},
"first_name": "Test",
"last_name": "Test2",
"custom_fields": {            
"job_title": "subscriber"         
},
}, {   
"ID": "102",
"role": {            
"subscriber": true         
},
"first_name": "Test 3",
"last_name": "Test 4",
"custom_fields": {            
"job_title": "testing"         
},       
}]
Also, you were not referencing the model fields correctly in your view.
Here is the updated working fiddle: http://jsfiddle.net/kmmmv83y/1/

You had a comma at the end of the last property, that will typically error everything out, the below JSON should work:
{ "ID":"102”,
"role":{“subscriber”:true},
"first_name”:”Test 3”,
"last_name”:”Test 4”,
"custom_fields":{ “job_title”:”testing”}
}

Related

DataTables - -how to get rowgrouping working with objects as data

I am using datatables and here is how my data looks like:
{
"data": [{
"request": {
"responsible": "Pete Jackson",
"valuta": " EUR",
"customer": "Jim Manner",
"office": "123 Houston",
"UNID": "9D574D34B9140D3CC1257B8E002A487E"
}
}, {
"request": {
"responsible": "Jane Awesome",
"valuta": " EUR",
"customer": "Christian Slater",
"office": "503 New York",
"UNID": "2444DAA352E89A44C1257B8E002A487F"
}
}]
}
The datatables columns I have defined as followed:
'columns': [{
data: 'request.office',
'render': function(data) {
return data;
}
}, {
data: 'request.responsible',
'render': function(data) {
return data;
}
}, {
data: 'request.customer',
'render': function(data) {
return data;
}
}
]
Now I want to apply rowGrouping according the following example that I have found: http://live.datatables.net/migixiqi/1/edit
However it uses the rows for grouping and its seems the columns defined as dataSrc are considered as objects cause I get 'No group' returned as row group label.
How can I send in a real value as source in the rowgroup definition instead of the (expected) column value?
Perhaps I dont understand what you are hoping to do, but you can just pass the JSON path to the rowGroup.dataSrc exactly as you do with columns.data :
rowGroup: {
dataSrc: 'request.customer' //just a guess you want to group by custumer
},
http://jsfiddle.net/tgsz78jk/
PS: render() callbacks are unnecessary unless you actually need to do something special with a columns content, sort, filter or search behaviour.

AngularJS $scope is undefined outside of .then function

I have a a form which includes select input but the thing is ng-options is not working
Select code
<select ng-model="selectedGender" ng-options="item.value for item in genderData">
I got the data from
ReferenceService.searchCategory("GENDER").then(function (response){
$scope.genderData = response.data;
console.log($scope.genderData);
})
This is the console.log($scope.genderData)
Array(2)
0:{referenceId: 1, category: "GENDER", key: "GENDER_KEY", value: "Male", $$hashKey: "object:3"}
1:{referenceId: 2, category: "GENDER", key: "GENDER_KEY", value: "Female", $$hashKey: "object:4"}
length:2
__proto__
:
Array(0)
but I have tried hard coding the data
$scope.genderData= [
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
}
];
and it worked! but I used ng-options="item.name for item in genderData">
btw don't mind the data I just searched it for fast use
EDIT:
I think I found the problem. Why is it undefined outside the function?
check this console
line 244 is undefined but line 242 is not?
store the response in var then out side the function assign that var to $scope. it is a callback issue.
If still it's showing same you can use localsorage to store and get the data. but this approach is not recomanded in angularjs. but if nothing happen then you can use this approcah also
That's because ReferenceService.searchCategory("GENDER") returns a promise.
A promise will wait until the data is resolved, and then move on to its success function callback. Which is the .then(function(response)).
So the code will get to the console.log on line 244 before the promise has finished, and $scope.genderData has been created and therefore is undefined
The console.log on line 242 will wait until the promise has been resolved, and calls the success callback function.
UPDATE:
Here an example how to correctly link the $scope to the factory.
Note that you must link to the object and not to its properties in your $scope.
Wrong:
$scope.gender = ReferenceService.data.genderData;
Correct:
$scope.gender = ReferenceService.data;
Example:
myApp.factory('ReferenceService',
function ()
{
var ReferenceService = {
// This is the global object. Link the $scope to this object
data: {
genderData: {}
},
searchCategory: function (type)
{
var getData = $http.get("URL", { params: { gender: type } }).then(
function (response)
{
// Store the data in the factory instead of the controller
// You can now link your controller to the ReferenceService.data
ReferenceService.data.genderData = response.data;
}
)
return getData;
}
}
return ReferenceService;
}
);
myApp.controller('MyController',
function ($scope, ReferenceService)
{
// Link $scope to data factory object
$scope.gender = ReferenceService.data;
// Now you only have to call the factory function
ReferenceService.searchCategory("GENDER");
// This will now log the correct data
console.log($scope.gender.genderData);
}
)

How to get Values of Response ReactJs

Hey guys im pretty new in developing with react.
I got some problems to fetch data from my json response
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response.data.name);
console.log(response.name)
});
{
"name": "mkyong",
"age": 30,
"address": {
"streetAddress": "88 8nd Street",
"city": "New York"
},
"phoneNumber": [
{
"type": "home",
"number": "111 111-1111"
},
{
"type": "fax",
"number": "222 222-2222"
}
]
}
The console shows me undefined.
Whats the problem why i cant get the data via plain js?
well because the response of an axios call is a string, so you have to parse it first,
axios.get('http://localhost:9000/test')
.then(function (response) {
const parsedResponse = JSON.parse(response);
console.log(parsedResponse.name));
});
In my case, something like this worked:
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response.data['name']);
});
There may be different levels of the json tree, so in my case I first tried displaying just response.data to see what's inside & got to the element this way.
Are you sure its not
axios.get('http://localhost:9000/test')
.then(function (response) {
console.log(response['data'][name]);
console.log(response[name])
});

how to get length of couch.get(data,headers,status)

I tried to create nodejs with expressjs by connecting to couchdb . For that I have used nodejs-couch installed using npm.
app.get('/', function(req, res) {
couch
.get(dbName, viewUrl)
.then(
function(data, headers, status) {
console.log(data.length);
var data1 = data.data.rows;
console.log(data1);
res.send(data1.length);
},
function(err) {
res.send(err);
}
);
});
my json data :
[
{
"id": "7a6a4d9cf76efc00977ca63ca3002a31",
"key": "7a6a4d9cf76efc00977ca63ca3002a31",
"value": {
"name": "nagarjuna",
"email": "nagarjuna#gmail.com",
"password": "12345",
"mobile": "987654321",
"rev": "8-c90103f1fca709ae314a684c767c97dc"
}
},
{
"id": "7a6a4d9cf76efc00977ca63ca3003743",
"key": "7a6a4d9cf76efc00977ca63ca3003743",
"value": {
"name": "sandeep",
"email": "sandeep#gmail.com",
"password": "54321",
"mobile": "9876541",
"rev": "1-fd6e667b87794adea78e169bc46016e6"
}
}
]
I had written get function to fetch all customer information. If I print console.log(data.data.rows); I am able fetch json data. But I want to get length and attribute values from json. But when I print console.log(data.length); it getting undefined.
Can any one suggest me. Any suggestions would be appreciated.
The result from the database contains a lot of different information, like meta information and the results. Here is a trimmed down representation of what you're looking for
var data = {
data: {
rows: [{row1},{row2}...];
}
}
So for you to be able to get the length of rows from the database, you'll have to dig into the right attribute in the object like this:
var length = data.data.rows.length;

How to get JSON objects from web service response

I am very new to use web services, javascript, JSON technologies and I need to use a URL to get some data to use in my HTML file.
The url that I am trying to get value of is something like this.
the result of this url in browser is like below:
{
"transactionid": "asdf",
"status": 0,
"poilist": [
{
"id": 123,
"name": "some company",
"address": "address",
"latitude": 333333,
"longitude": 333333,
"distance": 4869
},
{
.... // lots of similar nodes to above
}
}
I need to get some properties of poilist list such as longitude, latitude etc. and use them in my HTML file which includes only Javascript and HTML codes.
I made some research on internet but couldn't find a appropriate example for my situation. I don't know where to start. Any help will be appreciated.
Thank you.
You could to it this way:
var url = 'http://www.locationbox.com.tr/locationbox/services?Key=key&Cmd=PoiSearch&Typ=JSON&Latitude=30&Longitude=30&Radius=10000&Brand=SomeBrand&Keyword=';
$('#test').on('click', function () {
$.ajax({
url: url,
// the name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// tell jQuery we're expecting JSONP
dataType: "jsonp",
// work with the response
success: function (response) {
//response is an object. use it here
console.log(response); // server response
}
});
});
http://jsfiddle.net/hlapidez/sm64g/
Hope this helps for u.
Here poilist is an JsonArray.
So u have to iterate poilist and get poilist properties
javascript example
var response = "{ "transactionid": "asdf", "status": 0, "poilist": [ { "id": 123, "name": "some company", "address": "address", "latitude": 333333, "longitude": 333333, "distance": 4869 },... ";
var poiList = response.poilist;
for(var i=0;i<poiList.length;i++){
var name = poiList[i].name;
var id= poiList[i].id;
var lat = poiList[i].latitutde;
var long = poiList[i].longitude;
console.log(lat);
console.log(long);
}
This code will print all properties of poilist in browser's console.
I would start with jQuery, specificaly with jQuery.getJSON(). Read about it here.
If you haven't used jQuery and don't know how to use it. I would look at here first.
Very basic example of loading the data and showing them in console would look like this:
$(document).ready(function() {
var url = ""; //enter an url here
$.getJSON(url, function( data ) {
console.log(data);
});
});