Display Json from two nodes with Ajax - ajax

I am having difficulty to display data from json. Title shows fine but item.volumeInfo.industryIdentifiers.type returns undefined.
$.ajax({
url: 'https://www.googleapis.com/books/v1/volumes?q=:isbn=0-13-727827-6',
dataType: 'json',
success: function (data) {
$.each(data.items, function (index, item) {
$(".tab1").append("<div>" + item.volumeInfo.title + "</div><div>" + item.volumeInfo.industryIdentifiers.type + "</div>");
});
}
});
Fiddle is here: http://jsfiddle.net/HFs8U/1/
Really appreciate your help.

You need to put [0] before .type as a volumeInfo can have two industryIdentifiers. Naturally this will only show the first one so you may need to find a more appropriate way of showing both if more than one exist.
$.ajax({
url: 'https://www.googleapis.com/books/v1/volumes?q=:isbn=0-13-727827-6',
dataType: 'json',
success: function (data) {
$.each(data.items, function (index, item) {
$(".tab1").append("<div>" + item.volumeInfo.title + "</div><div>" + item.volumeInfo.industryIdentifiers[0].type + "</div>");
});
}
});

The solutions wasn't very difficult. david99world suggested to do another $.each an loop through each item and pointed to a fiddle. I have slightly amended it and came up with:
$.ajax({
url: 'https://www.googleapis.com/books/v1/volumes?q=:isbn=0-13-727827-6',
dataType: 'json',
success: function (data) {
$.each(data.items, function (i, item) {
$(".tab1").append("<div>" + item.volumeInfo.title + "</div>");
$.each(item.volumeInfo.industryIdentifiers, function (i2, type) {
$(".tab1").append("<div>" + type.type + "</div>");
});
});
}
});
http://jsfiddle.net/8SnaL/
Hopefully it may be useful for some body else.

Related

How to bind AJAX method's result value with html.?

AJAX method
Now , I want the count variable to bind with html and need help as i don't have enough knowledge of AJAX
$.ajaxSetup({
beforeSend: function (xhr) {
var token = localStorage.getItem('ngStorage-premier_agent_token')
var value = 'Bearer ' + JSON.parse(token)
xhr.setRequestHeader('Authorization', value);
}
});
$.ajax({
url: "/api/activity/listcount",
type: "GET",
success: function (data) {
console.log(JSON.stringify(data))
var count=data.count;
}
});
added a container where you want to print your HTML, like a div
<div></div>
and then append the result to that div on your success function
$.ajax({
url: "/api/activity/listcount",
type: "GET",
success: function (data) {
console.log(JSON.stringify(data))
$("div").append("result is" + data.count);
}
});

Syntax Error JSON file read with AJAX

I am getting a syntax error for the square bracket '[' from my JSON file. Given below is the JSON.
[
{
"Product Name":"CPE 10281",
"Application":"Tyres",
"Weight":150,
"Cost to Produce":5000,
"Grade":"A",
"Cost to Sell":40000,
"Strength":100
},
{
"Product Name":"CPE 10282",
"Application":"computers",
"Weight":250,
"Cost to Produce":4000,
"Grade":"H",
"Cost to Sell":25000,
"Strength":90
}
]
I am trying to use AJAX to read my JSON file.
$.ajax({
url: "dataProductJSON.json",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val) {
item.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},
});
I am running my html from Eclipse with Apache Geronimo as the server.
Please Help.
You are missing a { in the below line
success: function (data)
Make it
success: function (data) {
Edit
You are having parsing the data incorrectly , do it as below
$.ajax({
url: "test.html",
dataType: 'json',
mimeType: "application/json",
success: function (data) {
var item = [];
$.each(data, function (key, val){
$.each(val, function (innerKey, innerValue){
item.push('<li id="' + innerKey + '">' + innerValue + '</li>');
});
});
$('<ul/>', {
'class': 'interest-list',
html: item.join('')
}).appendTo('body');
},
});
You need to use 2 loop , one for the array and the other to loop through the object property
I tried things and it is working fine

display values returned by json

I have simple question to display data on html page. Following code displays array of json data on screen. but, I want to display it by each element such as "url", "img_url" and so on.
could you please let me know who to do it ?
ajax code
var dataString = 'url=' + pathname + '&img_name=' + img_name + "&tag=" + tag;
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
complete: function (xhr, status) {
if (status === 'error' || !xhr.responseText) {
//handleError();
alert("error");
} else {
var data = xhr.responseText;
$('#tt').html("<div id='message'></div>");
$('#message').html(data);
}
}
});
json return
{"cid":"14","url":"http:\/\/localhost\/","img_url":"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg","img_name":"mat_173317134c.jpg","html":"<div id=\"hotspot-19\" class=\"hs-wrap hs-loading\">\r\n<img src=\"http:\/\/static.naver.net\/www\/up\/2013\/0305\/mat_173330634c.jpg\">\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"95\" data-y=\"64\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nasdf\r\n<\/div>\r\n<div class=\"hs-spot-object\" data-type=\"spot\" data-x=\"168\" data-y=\"53\" data-width=\"30\" data-height=\"30\" data-popup-position=\"left\" data-visible=\"visible\" data-tooltip-width=\"200\" data-tooltip-auto-width=\"true\">\r\nrere\r\n<\/div>\r\n<\/div>\r\n","jscript":""}
$.ajax({
type: "POST",
url: "image_finder.php",
data: dataString,
dataType: 'json',
success: function (data) {
for(var item in data){
console.info(item);//print key
console.info(data[item]);//print value
}
}
});
I hope this is what you need.

CFWheels Pagination using AJAX

I've searched the entire Internet, all of it, and cannot find an answer to this.
I'm using the ColdFusion CFWheels Framework to query a database. The query is done via AJAX like this:
var id = $("#ship-id").val();
$.ajax({
type: "POST",
url: "/my-controller/my-method?format=json",
data: {shipId: id},
dataType: "json",
success: function(response) {
var resultHtml = '';
$.each(response, function(i, value) {
resultHtml += '<tr><td>' + value.FIRSTNAME + ' ' + value.LASTNAME + '</td></tr>';
});
$("#my-table").html(resultHtml);
}
});
I need to paginate that result set. In CFWheels you normally do that by setting the handle, page, perPage and order values in the query like this:
var order = model("order").findAll(
select="id, firstname, lastname, email",
where="orderid IN (#ValueList(orders.id)#)",
handle="ordersQuery",
page=params.page,
perPage=5,
order="lastname"
);
Then you just put this line in your view:
<cfoutput>#paginationLinks(handle="ordersQuery")#</cfoutput>
But... how in the heck can you get pagination to work with an AJAX call?
I think that something along these lines might be your answer (notice I removed the url and added those params to data ...
$.ajax({
type: "POST",
data: {
shipId: id,
controller: myController,
action: myAction,
page: params.page,
},
dataType: "json",
success: function(response) {
var resultHtml = '';
$.each(response, function(i, value) {
resultHtml += '<tr><td>' + value.FIRSTNAME + ' ' + value.LASTNAME + '</td></tr>';
});
$("#my-table").html(resultHtml);
}
});

issue with $ajax async false

I have this code:
for(var i in dataJson){
direcc=dataJson[i].dir ;
id=dataJson[i].id ;
$.ajax({
url: 'http://dev.virtualearth.net/REST/v1/Locations?query=' + direcc + '&key=mykey&jsonp=?',
dataType: 'json',
async: false,
success: function(result) {
loc = result.resourceSets[0].resources[0].point.coordinates;
direcc=result.resourceSets[0].resources[0].address.formattedAddress;
lat=loc[0];
long=loc[1];
$("#bing").append('latitudeBing:' + lat + '<p>longitudeBing:' + long+'$$$$$$'+id);
}
});
The problem is that I want to write latitude+longitude+ id, but when it writes the id is the last for all of them ( id goes from 1-200, and in all coordinates appears 200).
The original function was $getjson and i changed it to make the $ajax call async:false, but that doesn't fix my problem.
Could anybody help me please? Thank you very much!!
Edited with the correct answer now.
Fiddle to show the concept
for(var i in dataJson){
var direcc = dataJson[i].dir;
var id = dataJson[i].id;
makeAjaxCall(direcc, id);
}
function makeAjaxCall(direcc, id) {
$.ajax({
url: 'http://dev.virtualearth.net/REST/v1/Locations?query=' + direcc + '&key=mykey&jsonp=?',
dataType: 'json',
async: false,
success: function(result) {
loc = result.resourceSets[0].resources[0].point.coordinates;
direcc=result.resourceSets[0].resources[0].address.formattedAddress;
lat=loc[0];
long=loc[1];
$("#bing").append('latitudeBing:' + lat + '<p>longitudeBing:' + long+'$$$$$$'+id);
}
});
}
This puts the "current" direcc and id in the scope of the makeAjaxCall function. The Fiddle I added is just showing how.
Also, no need to keep the async: false, around, so feel free to remove that if you want.

Resources