So I have a list with the default title field and a custom number field I named "Hours". I want to be able to display the value of both fields on a page. Displaying them is the easy part, but I am having trouble retrieving them.
I am using this code:
var json = $.ajax({
url: "[URL]/_api/web/lists/getbytitle([LIST NAME)/items",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
})
I can then navigate the JSON to get to the results where it shows the information for each item in the list, but I can only find the "Title" field, not the "Hours" field. Also, I tried it on a separate site and was able to get the "Hours" data, but it showed up with an odd name (d.results[i].o0ci). I am not really sure what is going on here, so any help would be appreciated. Thanks!
First you should check the internal name of your column, let say that i have this structure on sharepoint:
Title | Hours | Has Free Time
well internally sharepoint tends to change the name of those columns to
Title | Hours | Has_x0020_Free_x0020_Time
or some times if you create the column but you delete it, this might happend:
Title | Hours1 | 0o13
So try this code to see your internal names:
$.ajax({
url: "[URL]/_api/web/lists/getbytitle([LIST NAME)/items",
type: 'GET',
headers: { "Accept": "application/json; odata=verbose" },
dataType: 'JSON',
success: function (data){
$.each(data.d.results, function (index, value) {
console.info(value) // You should see all sharepoint fields so you can check the correct fieldname of hours
console.info("Title: ", value.Title);
console.info("Hours: ", value.Hours);
}
}
});
Related
I am trying to the JSON values from a JQuery Ajax request into a dataset. The problem is that the dataset is in quotation marks and will not accept my parsed JSON values.
This is the original dataset:
{"value":"B", "number":"1492"},
{"value":"C", "number":"2782"},
{"value":"D", "number":"4253"},
{"value":"E", "number":"2702"}
I need for it to be accept the parsed JSON from the JQuery call.
$(document).ready(function(){
$("#ButtonClick").click(function(){
$.ajax({
url: "https://myUrl.com",
type: "get",
dataType: "json",
success: function(data) {
{"value":data.name, "number": data.currency},
{"value":data.nameA, "number":data.currencyA},
{"value":data.nameB, "number":data.currencyB},
{"value":data.nameC, "number":data.CurrencyC}
How can I go about doing this?
I want to show key/value pairs (of social relationship data count) in bubble using D3.js or something similar.
Here goes my output from REST client:
{"data":{"sister":3,"General":130,"mother":3,"guest":1,"brother":10,"general":1,"father":4},"messages":[]}
And here goes the Jquery code for the same. I am getting keys in 'arr' and values in 'values' in the following code. All I need to output the data in visual form as bubble as per weight, example, 'General' count is 130, so it should have biggest bubble. like that.
$(document).on("click", "#socialGraph", function(){
var id="4501905311187190264";
$.ajax({
url: "/getSocialGraph/"+id,
type: "GET",
contentType: "application/json",
dataType: "json",
success: function(data){
var arr=Object.keys(data.data);
var value=[];
for(var i in arr){
value.push(data.data[arr[i]]);
}
I would like to pass a Dictionary> to client using JavaScript.
I did look at this post and I didn't understand exactly how to proceed.
In case I'm doing something wrong I'll explain what I want to do.
The dictionary contains the 'name' key of all worksheets in the Excel file, and the 'value' is the column value of the first row in that worksheet.
The UI of the client should have two "drop list", the first will contain the key which is all the names of the worksheet in the Excel file.
The second contain all the column value of the first row of the worksheets that will choose in the first drop list – which is actually a List as the value in the dictionary.
So all the back end C# code is working fine. Now I need help in the front end JavaScript.
How do I parse the data to a key value so I can do a "search" on the keys as the client chooses some "key" in the first drop list so I can get back the relevant values as a list?
Thanx!
var ajaxRequest = $.ajax({
type: "POST",
url: "http://localhost:1894/api/Values",
contentType: false,
processData: false,
data: data,
success: function(dataTest) {
}
});
This is the JSON that I get back from the server:
{"First":["Name","Link","User","Pass"],"Sec":["test01"]}
How would I perform a search on this like in C#? I want to be able to do something like this: "dict.TryGetValue(key, out value); and the out value would return as an array of string or as a List.
Try this(you don't need var ajaxRequest variable you can directly call like this:
$.ajax({
type: "POST",
url: "http://localhost:1894/api/Values",
dataType: "json",
data: data,
success: function(dataTest) {
//dataTest should contain your data in a javascript object
for(var i = 0; i < dataTest.First.length; i++)
{
window.alert("" + dataTest.First[i]);
}
for(var i = 0; i < dataTest.Sec.length; i++)
{
window.alert("" + dataTest.Sec[i]);
}
//etc...
//this should print the values returned if you showed me exactly how your JSON is...
}
});
The javascript object will contain properties with an array as the value for each property. Think of it like a map of <String, String[]>. So your returned object dataTest will have properties First and Sec and for First the value associated with the key First will be ["Name","Link","User","Pass"] which is just an array. Same for Sec. So `dataTest.First[0] will equal "Name" and dataTest.First[1] will equal "Link" etc...
*****************************************UPDATE**************************************
You can save your dataTest to a global variable in this example (myObject) then you can access like this:
var key = "First";
// Or if you want to get your key from a dropdown (select) element then you could do like this:
var key = document.getElementById("myselect").options[document.getElementById("myselect").selectedIndex].innerHTML;
if(myObject[key] != undefined)
{
//That means there is values for this key.
//Loop through values or do whatever you want with myObject[key].
for(var i = 0; i < myObject[key].length; i++)
{
window.alert("" + myObject[key][i]);
}
}
$.ajax({
type: 'POST',
url: 'url',
data: val,
async: false,
dataType: 'json',
success: function (max) {
console.log(max.origin);
}
});
Then i am getting an output which is
["test,origin"]
I want to split it like .
test
origin
Please suggest me .
Much appreciated
If your returned value of max.origin really is ["test,origin"], you have to do something like this
var data = max.origin[0].split(',');
so that data will contain a list of the elements that are comma-separated. However, if your max.origin is in the form of ["test", "origin"], you can simply select each/all items by going through a for-loop and (optionally) print them out:
for (var i = 0; i <= max.origin.length; i++) {
console.log(max.origin[i]);
}
If you know that you only get the two elements (test and origin) each time, #void's answer is a good approach.
max.origin[0].split(",")[0] will give you test
max.origin[0].split(",")[1] will give you origin
I have found several posts similar to this topic but nothing I have tried has actually worked. My question is simple and should be easy to answer. My return json object will have one key and one value. For my code below, the alert shows "[{"DISCOUNT":1}]" and all I am trying to do is parse out the "1" and display it. Ultimately, I want to plug that value into a variable and use for multiplication, but I can't even get the darn number to display by itself. My code is below:
function codeMatchTest() {
if ($('#dbReturnString').val() == '') {
alert("Please enter a discount code.");
} else {
$.ajax({
type: "POST",
url: "PROMO.svc/MatchCode",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ codeInput: $('#dbReturnString').val().toLowerCase() }),
success: function (json) {
alert(json.d);
/*alert (json.d.discount); // getting "undefined"
$.each(json.d, function (key, value) {
var discount = value;
});
alert("Success: " + discount); //getting "undefined" */
},
error: function () {
alert("There was an error with your request.");
}
});
}
}
I have found no good references on how to actually use the data in a json object. My json object will only consist of a single key and value and I will only ever need to use the value.
Also, I have tried several iteration using $.each and none work. Based on the jquery documentation, it should be very easy but I am having not luck.
If your alert is showing "[{"DISCOUNT":1}]" that means you have an object within an array.
try alert(json.d[0].DISCOUNT);
JSON parsed objects are case sensivetive, plus its seems that json.d contains a string (wich seems to be in json) rather than an object. Try:
var discount = JSON.parse(json.d);
discount = discount[0].DISCOUNT;
success: function(json) {
alert(json.d[0]["DISCOUNT"]);
}
First comment on your code, you are reinventing what jQuery does.
data: JSON.stringify({ codeInput: $('#dbReturnString').val().toLowerCase() }),
It should just be
data: { codeInput: $('#dbReturnString').val().toLowerCase() },
Now to get the data it is simple, you are returning an array with an object in it.
Let us look at it as a regular variable and not an Ajaqx call.
var json = [{"DISCOUNT":1}];
So you got an array? How do you get the object inside of it? You reference the index. Since you said there will only be one index being returned, than use [0] to access it.
success: function (json) {
alert(json[0].DISCOUNT);
To access the first item from the json you may use
alert(json.d[0].DISCOUNT);
Because json.d is an array and it contains one object which is 0 index. So, json.d[0] will select the first item/object from the array and using .DISCOUNT you can access the object's property. It's also possible to access the property like
alert(json.d[0]["DISCOUNT"]);
Try this way
You can use JSON.parse(json.d) / json.d
var data = json.d;
for(i=0;i<data.length;i++) {
alert(data[i].fieldname);
}