How to display a JSON Object with ListView - javascript

I am trying to display a json object with listView using the code below and have no error message but it doesn't works.
return WinJS.xhr({
url: "http://rest-api.wasamundi.com/search/?q=" + userInput
}).then(function (response) {
try {
var result = response.responseText;
var obj = eval("(" + result + ")");
}
catch (e) {
}
//code pour objet recuperer du web
var dataArray = result;
//create object for data obtained from the web
var dataList = new WinJS.Binding.List(dataArray);
var publicMembers =
{
itemList: dataList
};
WinJS.Namespace.define("DataResult", publicMembers);

Related

AJAX Call passing null values to controller

I'm trying to send data to the controller using this AJAX call of type POST but it's sending null values to the controller. Through this code I want to make new records in the Microsoft Dynamics 365 CRM database.
var formdata = new FormData();
var rowCount = $(".newRow").length;
var projectarray = [];
var datearray = [];
var amountarray = [];
for (var i = 0; i < rowCount; i++) {
projectarray[i] = $("#ProjectType_" + i).val();
datearray[i] = $("#ClaimExpenseDate_" + i).val();
amountarray[i] = $("#Amount_" + i).val();
}
formdata.append("projectarray", projectarray);
formdata.append("datearray", datearray);
formdata.append("amountarray", amountarray);
$.ajax({
url: "#Url.Action("SetClaimDetails", "Claim")",
type: "POST",
data: formdata,
processData: false,
contenttype: false,
success: function (data) {
debugger;
window.location.href = "ess/claim";
alert("Submit Successful!");
},
error: function (err) {
window.location.href = "";
alert("Submit Failed!");
}
});
Here is my controller method which is storing null values instead of the values passed by the AJAX call. And because of this I'm not able to create new records in the database.
public ActionResult SetClaimDetails(string projectarray, string datearray, string amountarray)
{
try
{
Entity item = new Entity("bam_expenseclaims");
item["bam_expdate"] = Convert.ToDateTime(datearray);
item["bam_amount"] = amountarray;
item["bam_project"] = new EntityReference("new_project", Guid.Parse(projectarray));
globalService.Connection.Create(item);
}
catch (Exception ex)
{
XmlConfigurator.Configure();
ILog log = LogManager.GetLogger("TechnicalErrorAppender");
log.Error(string.Empty);
throw ex;
}
return View(Constant.CLAIMPATH);
}

Console.log cannot show in jQuery

My javascript is using jQuery. However, when I clicked the #button_execute, the console.log in the callback function of .done didn't print anything on my console. I am not sure how can I console.log them.
$("#button_execute").click(function() {
var htmlContainer = htmlGraphContent();
var graphContext = returnGraphContext();
var formData = extractFormData();
var buildingId = formData.buildingId;
var zoneId = formData.zoneId;
var dateExtracted = formData.dateExtracted;
var url = "/api/building/" + buildingId + "/zone/" + zoneId + "/" + dateExtracted;
sendJqueryRequest("GET", url)
.done(function(data) {
console.log("#button_execute - data.data.result:",data.data.result);
buttonPlayLayout();
var resultArray = data.data.result;
htmlContainer.collapse("show");
lastDataRetrieved = resultArray;
console.log( "#button_execute_bms - sendJqueryRequest(\"GET\"," + url + ")");
startRealTimeGraph(resultArray, graphContext, formData);
console.log("real_time_graph.js - #button_execute_bms - lastDataRetrieved:",lastDataRetrieved);
})
.fail(function(error) {
alertCreator(error, "#bms-container", "bms-alert");
});
});
This is sendJqueryRequest
var sendJqueryRequest = function (type, url, data) {
var jsonToSend = {type: type, url: url};
console.log("sendJqueryRequest");
if(data) {
jsonToSend.data = data;
console.log("sendJqueryRequest jsonToSend: "+jsonToSend);
}
console.log(jsonToSend.url);
return $.ajax(jsonToSend);
};
Thanks.
I realised that's because of a really silly situation: I didn't realise that the console.log of front-end js will post to the console of the browser, not the console of the terminal. Sorry for wasting you guys so much time on this issue.

unable to log data into database table using node js

DatabaseConnection.prototype.add_user_transaction =function(tournamenId,userId, callback ){
var dbConn = this;
var insertObject = {
//data
};
var ticketModel = dbConn.rummyConnection.model('tournament_user_transactions');
var transactionPromise = ticketModel.update(insertObject ,{where: {tournament_id: tournamenId, user_Id: userId}}).then(function(result) {
callback({status:"SUCCESS", result: result});
}).catch(function(err){
debug(err.stack);
var error = new Error('Error ');
callback({status:"ERROR", error:error});
});
};
// On debug result is[0], record is not inserting into table.

Using parse.com is it possible to convert a string so that it saves as a pointer in the object browser?

Using parse.com and JavaScript.
Currently I have a BadgeSentTo which is a string taken from a html option box. I want to save this to parse, but ideally I want to save it into a pointer column "SentTo" so that it links back to the _User class.
It wont let me save as is, because its expecting a pointer. Is there a why to convert this to a pointer in the code?
$(document).ready(function () {
$("#send").click(function () {
var myBadge = new MyBadge();
var badgeselected = $('#badgeselect img').attr("src");
var BadgeSentTo = $('#SentToUser').val();
var uploadercomment = $('#UploaderComment').val();
myBadge.set("BadgeName", badgeselected);
myBadge.set("Comment", uploadercomment);
myBadge.set("uploadedBy", Parse.User.current());
myBadge.set("SentTo", BadgeSentTo).id;
myBadge.save(null, {
success: function (results) {
console.log("Done");
//location.reload();
},
error: function (contact, error) {
// The save failed.
alert("Error: " + error.code + " " + error.message);
}
});
return false;
});
});
The query capturing the data is
var currentUser = Parse.User.current();
var FriendRequest = Parse.Object.extend("FriendRequest");
var query = new Parse.Query(FriendRequest);
query.include('toUser');
query.include('SentTo');
query.include("myBadge");
query.equalTo("fromUser", currentUser);
query.equalTo("status", "Request sent");
query.find({
success: function (results) {
var friends = [];
for (var i = 0; i < results.length; i++) {
friends.push({
username: results[i].get('toUser').get('username'),
userId: results[i].get('toUser').id
});
var select = document.getElementById("selectNumber");
$.each(friends[0], function (i, v) {
//alert(i+" "+v);
var opt = v;
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
})
}
If BadgeSentTo contains the objectId of the User, you'll need to wrap that in a Parse Object. The SDK will convert it to a pointer to _User when it saves.
myBadge.set("SentTo", new Parse.User({id: BadgeSentTo}));

Node.js callback confusion

I am trying to implement an autocompleter on a nodejs app using nowjs.
everyone.now.sendAutocomplete = function(search) {
var response = getAutocomplete(search);
console.log("response");
console.log(response);
};
which calls:
function getAutocomplete(search) {
console.log(search);
var artist = new Array();
request({uri: 'http://musicbrainz.org/ws/2/artist/?query=' + search + '&limit=4', headers: "Musicbrainz Application Version 1"}, function(error, response, body) {
par.parseString(body, function(err, result) {
var count = result['artist-list']['#']['count'];
var artists = result['artist-list']['artist'];
// var artist = new Array();
if (count > 1) {
artists.forEach(function(a) {
var att = a['#'];
var id = att['id'];
var name = a['name'];
var dis = a['disambiguation'];
if (dis) {
var display = name + " (" + dis + " )";
} else {
display = name;
}
artist.push({'id':id, 'name': name, 'disambiguation':dis,
'label':display, 'value':name, 'category':"Artists"});
});
//everyone.now.receiveResponse(artist);
console.log("artist");
console.log(artist);
return artist;
} else {
console.log(artists);
var att = artists['#'];
var id = att['id'];
var name = artists['name'];
var dis = artists['disambiguation'];
var resp = [{'id':id, 'name': name, 'disambiguation':dis,
'label':name, 'value':name, 'category':"Artists"}];
return resp;
// everyone.now.receiveResponse([{'id':id, 'name': name, 'disambiguation':dis,
// 'label':name, 'value':name, 'category':"Artists"}]);
}
});
});
}
However, console.log(response) says that response is undefined. I am new to node so the answer is probably simple, but still can't figure it out.
You are treating the async call as synchronous. Your getAutocomplete needs to take a callback function to get the response. You're using that a lot already, in your request call and your parseString call.
Like this:
everyone.now.sendAutocomplete = function(search) {
getAutocomplete(search, function (response) {
console.log("response");
console.log(response);
});
};
And instead of return:
function getAutocomplete(search, callback) {
// ...
callback(result);
// ...
}

Categories