get the notification length depends on ReadFlag - javascript

JS:
function getJSON(){
$.ajax({
url: "getgroupednotification.json",
type: "GET",
crossDomain: true,
success:function(res){
$.each(res,function(index, value){
//console.log(res);
//console.log(value);
$.each(value.Notifications, function(index_, value_){
if(value.Source == 'CIRIS'){
var i = value.Notifications.length;
if(value_.ReadFlag != 1){
}
}
});
});
});
i want to get the notification.length if ReadFlag == 0. Im using Source == CIRIS for this example.
This is the link to my JSON
https://api.myjson.com/bins/navph

You could use something like this:
function getJSON() {
$.ajax({
url: "https://api.myjson.com/bins/navph",
type: "GET",
crossDomain: true,
success: function(res) {
var lens = res.filter(function(item) {
// filter by source
return item.Source === 'CIRIS';
}).map(function(item) {
// get only Notifications
return item.Notifications;
}).map(function(notification){
// get lengths of notifications which have at least 1 RedFlag not 0
return (notification.filter(function(item){
return item.RedFlag !== 0;
}).length)
})
console.log(lens[0]);
}
})
}
getJSON();

Related

Ajax request showing error

I am getting an error on ajax call - "No handler found for "undefined". Attached the screenshot in the following. I'm running this on react-redux application.
Had any of you guys faced the same issue? Any help? The code is given in the following:
export function updateBrandAdmin(data) {
console.log(data);
var brand_admin_data = data;
var updateUrl = `<url for ajax request>`;
return function(dispatch) {
var brandAdminData = new FormData();
//append description
if(data.description && (data.description != "")) {
brandAdminData.append("description", brand_admin_data.description);
}
//append banner name
if(brand_admin_data.brand_name && (brand_admin_data.brand_name != "")) {
brandAdminData.append("name", brand_admin_data.brand_name);
}
//append products
if (brand_admin_data.productids && brand_admin_data.productids.length > 0) {
for(var j=0; j < brand_admin_data.productids.length; j++) {
brandAdminData.append("products_ids[]", brand_admin_data.productids[j]);
}
}
//append logo
//The issue was here when I wasn't giving this check
if (brand_admin_data.logoAttribute.files[0]) {
brandAdminData.append("logo", brand_admin_data.logoAttribute.files[0]);
}
//append banner attributes
if (brand_admin_data.banners_attributes) {
// formData.append("video_attributes[]", null);
// if(brand_admin_data.banners_attributes.length > 0) {
// for(var i=0; i < brand_admin_data.banners_attributes.length; i++) {
brandAdminData.append("banners_attributes[]", brand_admin_data.banners_attributes[0].files[0]);
// }
// }
}
// console.log(data);
// console.log(updateUrl);
$.ajax({
type: "PUT",
url: updateUrl,
data: brandAdminData,
headers: getToken(),
success: function(resp, status) {
console.log(resp);
},
async: true,
cache: false,
contentType: false,
processData: false,
timeout: 60000
});
}
}

how to append data in after success in laravel

},
submitHandler: function (form) {
var form_data = $(form).serialize();
console.log(form_data);
$.ajax({
type: 'post',
url: SITE_URL+url,
data: form_data,
success: (function (json) {
if (json.type == "success") {
$.each(json, function(i, item) {
$('#div1').append("value"+item.name+) ;
})
var type = 'Created';
if(json.value===1 && typeof json.value != 'undefined'){
type = 'Update';
}
//$("#showDetail form").append();
resetform($(form).attr('id'));
successMessage('Subscription '+type+' Successfuly');
getPosts('show='+$('.custome-show-option select').val()+'&page='+$('#PcurrentPage').text());
} else {
$("#error_div").html('Module already exist');
}
}),
error: (function (response) {
$("#error_div").html('Error in processing');
})
});
this code is proper work but not show append data of form data where success::function(json) {} where j console.log(json) show output only 1
I don't know what is actually your problem but:
in your javascript code you should add:
dataType:'json'
In laravel in controller you should have something like this:
return response()->json(['type' => 'success', 'value' => 1, 'items' => $arrayOfItems]);
This part should be something like this:
$.each(json.items, function(i, item) {
$('#div1').append("value" + item.name) ;
});

How to repeat ajax request depending return

I have this code :
.on('finish.countdown', function() {
var onEndAuction = function () {
$.ajax({
type: "POST",
url: "{{path('app_auction_end')}}",
data: {auctionId:{{ aReturn.oAuction.getId()}}},
success: function (data) {
console.log(data);
if (data == 0) {
setTimeout(onEndAuction, i_timer);
} else {
document.location.reload(true);
}
}
});
};
});
I want if data == 0 need to make another call on app_auction_end after 10 sec. Can you help me please ? Thx in advance and sorry for my english
Give the operation a named function:
var someFunction = function () {
$.ajax({
//...
});
};
Which you would then use for your .on() call:
.on('finish.countdown', someFunction)
And in the success handler, set a timeout for that function:
if (data == 0) {
setTimeout(someFunction, i_timer);
}
.on('finish.countdown', function() {
var onEndAuction = function () {
$.ajax({
type: "POST",
url: "{{path('app_auction_end')}}",
data: {auctionId:{{ aReturn.oAuction.getId()}}},
success: function (data) {
console.log(data);
if (data == 0) {
setTimeout(onEndAuction, i_timer);
} else {
document.location.reload(true);
}
}
});
};
//do our initial call otherwise it will never get called.
onEndAuction();
});

Why Html dialog opening data from cache

I have dilaog box which loads data in success function of an ajax call as follows.
$.ajax({
url: '#Url.Action("GetPolicyPremiumAllocation", "Policy")',
data: { policyID: selPolicyId },
type: 'POST',
success: function (data) {
if (data.length > 0) {
alert(data);
document.getElementById("modal_dialog").innerHTML = "";
// $("#modal_dialog").empty();
$("#modal_dialog").load(data,function( ) {
$("#close-button-id").on("click", CloseDialog);
});
$("#modal_dialog").dialog("open");
}
}
});
First time the div of dilaog is loading correct data.But Second time,it just shows the old data. I have tried to clear cache in the index action of my controller.Unable to figure out how to solve this.Please help.
Set ajax cache option to false:
$.ajax({
url: '#Url.Action("GetPolicyPremiumAllocation", "Policy")',
data: { policyID: selPolicyId },
cache:false,
type: 'POST',
success: function (data) {
if (data.length > 0) {
alert(data);
document.getElementById("modal_dialog").innerHTML = "";
// $("#modal_dialog").empty();
$("#modal_dialog").load(data,function( ) {
$("#close-button-id").on("click", CloseDialog);
});
$("#modal_dialog").dialog("open");
}
}
});
Or decorate your Action with [OutputCache(NoStore = true, Duration = 0)]

Ajax request return value

I have an Ajax call to my controller action looks like this:
var m = $.ajax({
mtype: "GET",
url: '#Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false
});
As u can see i return actionparametter to my controller and controller have to return either true or false which has to be my "m" value, but i cant get it to work. Any ideas?
Controller Code:
public virtual JsonResult GetBrandForValidation(string actionparameter)
{
var vendorId = _service.GetVendorIdByUsername(GetUserName());
bool k;
var brands = _service.GetBrandsByVendor(vendorId);
var brand = new BrandsViewModel();
brand.BrandName = "Opret ny Brand...";
brands.Add(brand);
foreach (var brandsViewModel in brands)
{
if (brandsViewModel.BrandName == "Intet")
{
brandsViewModel.BrandName = "";
}
}
var list = brands.Select(s => s.BrandName);
if (list.Contains(actionparameter))
{
k = true;
}
else
k = false;
return Json(k,JsonRequestBehavior.AllowGet);
}
And full function code :
var checkBrands = function(value, colname) {
var m = $.ajax({
mtype: "GET",
url: '#Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false
});
if (m == true)
return [true, ""];
else
return [false, "Brand eksistere ikke"];
};
Im quite new and very awfull at javascript, so dont judge hard
Is the ajax call being made in JavaScript? If so, mtype should be type.
var checkBrands = function(value, colname) {
$.ajax({
mtype: "GET",
url: '#Url.Action("GetBrandForValidation")',
data: { actionparameter: value },
async: false,
cache: false,
success: function(data){
if(data == 'm'){
//do something
}else{
//do something
}
}
});
};
SOLVED
var checkBrands = function (value, colname) {
var m = $.ajax({
mtype: "type",
url: '#Url.Action("GetBrandForValidation")',
async: false,
cache: false,
data: { actionparameter: value }
}).responseText;
if (m == 'true'){
return [true, ""];
}
else return [false, "Brand eksistere ikke"];
};

Categories