Adding JSON results within quotation marks in a dataset - javascript

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?

Related

Post data $(form).serialize() and an array together

I'm having trouble sending the form via ajax. In addition to the fields populated by the user, I need to send an array of objects together.
AJAX POST:
submitHandler: function (form) {
$.ajax({
url: urlPost,
type: "POST",
dataType: "json",
data: $(form).serialize() + JSON.stringify(myArray),
success: function (resposta) {
alert('success');
}
});
}
If I send only $(form).serialize() I can, but with the array not.
ARRAY:
myArray = [{name: 'a', node: 1}, {name: 'b', node: 12}, ...];
Change your data: $(form).serialize() + JSON.stringify(myArray) to data: $(form).serialize() + "&arr=" + JSON.stringify(myArray). For Further help refer to https://stackoverflow.com/a/10398820/4518930
.serialize() + Some JSON String doesn't make sense.
According to the docs:
https://api.jquery.com/serialize/
The .serialize() method creates a text string in standard URL-encoded
notation.
So you're really taking a string like foo=bar&goat=baz and then adding a JSON string to like. Which makes no sense.
I think you'd be better off serializing the form yourself into a JSON object. Adding another key for your array and then dumping that object to JSON via JSON.stringify and that string is your request data.
After the tip of Cody Caughlan, I created an object that adds all the properties of the form and the array itself. The code looks like this:
var dataForm = {};
$($(form).serializeArray()).each(function(index, obj){
dataForm[obj.name] = obj.value;
});
dataForm["MyArray"] = myArray;
And ajax: post: dataForm.

jquery POST data append not happening

I want to append some string before data output. Please help me figure out in how to do it. I have tried both HTML and append function, but I am not getting desired output.
Like if data = abc, it should show data = xyzabc and append xyz automatically to JSON data
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
url: "a.php",
data: data,
success: function(data) {
$(".the-return").html(
data
);
just add the string "xyz" to the output data as "xyz" +data
try
$(".the-return").html( "xyz" + data );

Trouble retrieving list values in SharePoint

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);
}
}
});

Parse key- value as weighted bubble output

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]]);
}

Split Json data into multiple data using Jquery

$.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

Categories