c3 charts - Getting values from controller but not displaying in chart - ajax

Created an application which contains some charts. Here is my code which gets values from controller(no error in console or in code), but values not displaying in chart.
function bmi()
{
var measuresArray = [];
var dimensionArray = [];
var acc = $("#accountName").val();
var year = $("#yearBased").val();
var dat = JSON.stringify({"accountName" : acc,"yearBased" : year});
$.ajax({
type : "POST",
url : "xyz.html",
cache : false,
data : dat,
async : false,
contentType : "application/json",
dataType : "json",
success : function(dataList) {
$.each(dataList,function(i,dataList){
measuresArray.push(dataList);
dimensionArray.push(i);
});
$("#bmiPriorityDiv").empty();
chartGenerator("#bmiPriorityDiv",measuresArray,dimensionArray,"Month",'bar');
}
});
}
Value i am getting like this :
measuresArray : Array(12)
0:0.6365796
1:0.6107492
2:0.5553435
3:0.7089947
4:0.6671214
5:0.7635135
6:0.6294821
7:0.728863
8:0.8280423
9:0.7690531
10:0.65411764
11:0.5833333
and for dimensionArray:
Array(12)
0:"June"
1:"October"
2:"December"
3:"May"
4:"September"
5:"March"
6:"July"
7:"January"
8:"February"
9:"April"
10:"August"
11:"November"
But i am getting empty chart. Please help...

Related

How to solve the following error in jQuery ajax while using datatable

Trying to display json data from PHP into a jQuery datatable, I get an error.
This is my code:
function fetchProjectTable()
{
$.ajax({
url: Domain+"/include/process.php",
method: "POST",
data: { getProjectResult: 1 },
success : function(data)
{
//var o = JSON.parse(JSON.stringify(data));
//var o = JSON.parse(data);
$('#project_table').dataTable({
data : data,
columns: [
{"data" : "id"},
{"data" : "p_name"},
{"data" : "p_amount"}
],
});
//console.log(o);
/*alert(o);
console.log(o);*/
}
});

Retrieving data from AJAX function for another outside function

I need to get a new file name using an ajax call so I can use it in another function. This is the original call for the new filename I am trying to retrieve from the setup_file function. The file array has all the data I need including the original file name.
var newfilename = setup_file(file);
But when I try to return the data I need from said function, it doesn't work.
function setup_file(file) {
var newfilename;
var newtitle = jQuery("#choosetitle").val();
var aspect = jQuery("#chooseaspect").val();
var uploadlanguage = jQuery("#uploadlanguage").val();
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type : 'POST',
async: false,
datatype : 'JSON',
data : {action : 'process_uploads',fileinfo: file,filetitle : newtitle, aspect : aspect, uploadlanguage : uploadlanguage},
success : function(data){
var json = JSON.parse(data);
var newfilename = json['filename'];
alert(newfilename);
//this alerts fine.
}
});
return newfilename;
//thisreturnsnothing, however if I change it to return 'Whatever'; it does return Whatever.
}
You created, same variable again in the success block, that cause the outer variable empty. Change the
var newfilename = json['filename'];
to
newfilename=json['filename']
In the success block.

iterate Jquery var list

items is a variable i used in below function...
items value is
[{"daLevel":"DA0","daName":"Da Name 0"},{"daLevel":"DA1","daName":"Da Name 1"},{"daLevel":"DA2","daName":"Da Name 2"},{"daLevel":"DA3","daName":"Da Name 3"},{"daLevel":"DA4","daName":"Da Name 4"},{"daLevel":"DA5","daName":"Da Name 5"},{"daLevel":"DA6","daName":"Da Name 6"},{"daLevel":"DA7","daName":"Da Name 7"},{"daLevel":"DA8","daName":"Da Name 8"},{"daLevel":"DA9","daName":"Da Name 9"},{"daLevel":"DA10","daName":"Da Name 10"}]
I need to display daName values in a select box as dropdown.
I am not able to get daName values from items var.
Any suggestion would be of gr8 help.
Thanks in advance
function notifyDa(excessId) {
alert("notified");
var html = "<table><tr><td align='center' colspan='2'> Excess Notification </td></tr><tr><td>Select DA Holder</td><td><select id='daList'>";
var ctx = '${contextPath}';
var queryUrl = ctx + "/excessList.htm?getDaList=true";
$.ajax({
url : queryUrl,
type : "POST",
dataType : "text",
success : function(result) {
alert(result);
**var items = result;
alert("items *** "+items);
alert("items[0] *** "+items.daName[0]);**
$('#notifyDiv').empty();
$('#notifyDiv').html(html);
$("#pop").click(function() {
$("#notifyDiv").fadeIn(1000);
if (!$("#notifyDiv").is(':visible')) {
return;
}
});
$("#notifyDiv").css({
left : ($(window).width() - $('#notifyDiv').width()) / 2,
top : ($(window).width() - $('#notifyDiv').width()) / 7,
position : 'absolute'
});
},
error : function() {
}
});
}
You can try this
result = jQuery.parseJSON(result); // as dataType is Text
items[0].daName;
You need to change your Datatype to JSON, then you can use jQuery .each() function for parsing the JSON you are getting. Something like this:
$(items).each(function(index,element){
alert(element.daName);
});
This will iterate through all the objects and gives you the required fields.

I want the returned data to be written to the div tag of the html

function showPrice(data) //pass the data as an object literal instead of a string
{
var $remaining = $('#remaining');
$remaining.empty();
$.ajax({
url: 'getevent.php',
data: data,
success: function(reponse){
$remaining.html(reponse);
}
});
}
$('#events').change(function(){
var pluspoint=$('#events').val();
var data = { q : 1};
showPrice(data);
});
I am trying to pass variable q to a php file and get back the result . I am getting the result but I am getting an error paramete q is undefined .
You can use JSON.stringify:
function showPrice(data) //pass the data as an object literal instead of a string
{
var $remaining = $('#remaining');
$remaining.empty();
$.ajax({
url: 'getevent.php',
data: data,
success: function(reponse){
$remaining.html( JSON.stringify(reponse) );
}
});
}
$('#events').change(function(){
var pluspoint=$('#events').val();
var data = { q : 1};
showPrice(data);
});

Jquery ajax is not able to send data to mvc3 controller action

I am working with MVC3. I have a javascript function which makes an ajax call to mvc3 controller action. When i send null in the data, it calls the controller action. but when I try to send location in data it gives javascript error i.e. 'this is not defined'.
function getPictureContent(location)
{
var pictures = getLocationPictures(location);
var content = "<div id=markerpictures></div>";
return content;
}
function getLocationPictures(location) {
var pics;
$.ajax({
type : "POST",
url : "/Home/GetLocationPictures",
data : {'location' : location},
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : false,
success : function (data) {
pics = data;
}
});
return pics;
}
Here is the controller action:
public JsonResult GetLocationPictures(string location)
{
List<string> pictures = new List<string>();
return Json(pictures);
}
Try this.. Location should be string.Be sure that will string.
function getLocationPictures(location) {
var pics;
$.ajax({
type : "POST",
url : "/Home/GetLocationPictures",
data : JSON.stringify({'location' : location}),
contentType : "application/json; charset=utf-8",
dataType : 'json',
async : false,
success : function (data) {
pics = data;
}
});

Resources