Consuming JSON with JavaScript [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm trying to read this link.
http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1
with this method:
$.getJSON(" http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1",function (json) {
alert(json.length);
});
But it doesn't work, and I don't know the reason :(
Some one can help me?
[edit]
I just want to read, the "json.length" its was just a test. I tried this with another .json, and worked. I want to know if a need to use some method that read without problems.

If the variable fetched is an object, instead of array, you need to use Object.keys(), which gives you the list of keys as an array and you can use the .length on it:
$.getJSON("http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1", function(json) {
alert(Object.keys(json).length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
One thing I noticed is, the following URL:
http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1
Doesn't have the CORS enabled. So, if you try accessing it, it throws an error in the Console, which is kinda silent. If you mean that, you need to request or make the JSON to have CORS Enabled.

Related

How to use javascript variable in php code in javascript function itself? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 4 years ago.
I want to get php array element using javascript variable as an index of a php array. I have tried this below method but it doesn't work.
// I want to get value of $arr[0] and here array index is a javascript variable
<script type="text/javascript">
function select_seller()
{
d = document.getElementById('seller_id').value;
document.write("<?php echo $arr['<script>document.write(d)</script>'];?>");
}
</script>
Please Help me
You can't do this. PHP code is executed first so once the user loads the page, there's no way for PHP to detect a JavaScript variable.
If you posted some more code we could probably tell you how to do what you are trying to do in JavaScript.
You don't seem to know the difference between server-side and client-side, your php code is interpreted on the server to produce html data and send it to the browser to render it to the user, hence you cannot use php function on the client-side .. php role is preparing and sending data to the client-side.
so whatever you are trying to achieve here you have the option of using ajax calls to get data from the server and process it with js upon receiving it.

Variable keeps turning into an object instead of an Array in Javascript

I'm learning javascript (and HTML on Electron), and I have a variable which is meant to be an array:
var arrayList = [];
When I add objects to it (taken from JSON data), using Push:
arrayList.push(object);
Everything works perfect. I then save this using an api from npm called: electron-json-storage
I then want to pull this data, push an object into it and save it back. The problem is when I use the api to obtain the data to save into the variable arrayList, it turns arrayList into an object, it then errors out when I try to push to it as it is the wrong variable type now compared to before.
Would love any help and guidance would be great, thanks!
apologies about this, turns out the 'electron-json-storage' api sends back a blank object if the file doesn't exist, which was actually my problem (even though I know I didn't write that above) and what kept turning it into an object. I wouldn't of figured it out without re-looking over my code before posting here (so thanks klugjo) and for trying to parse the object which double confirmed it was an object and lead me to find the actual problem (So thanks so much Robert).
Again thanks all for the help!

Why does jQuery.data() not update the element [duplicate]

This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 6 years ago.
Im having an issue or possibly a expected result with JQuery's .data() method. Im unsure why when using the element selector the data values are not updated after running the data method. See the screenshot for an example of what im talking about.
Im unsure why when using the data(key,value) method, it updates the elements data with the expected output. However why is it that it doesnt updated the values html attribute? My knowledge in Javascript is still very limited, but i'd love to know what this is and if i should expect this.
jQuery reads all data-attributes on pageload. It then deals with changes by the .data()-function only in it's storage.
To update the "real" HTML-Attribute, you need to manipulate the content like so:
$('.selected a span:eq(3)').attr('data-value', 'newValue');

Single Item Array Read as Object in React

I'm hoping someone can help me bug fix an issue I'm having where an array that contains a single item is read as an object in json data. I'm using .mapto loop over the values like defined here: https://facebook.github.io/react/docs/multiple-components.html
The build I'm using is: https://github.com/newtriks/generator-react-webpack, so I'm not sure if this is a webpack issue, fetch issue (how I'm requesting the data), or something much more obvious. Any insight would be appreciated. Thanks.
I found where the error was. I was using YQL as a proxy service to get around CORS. Turns out the default response is “lossy”, where single element arrays are returned as an object.. had me scratching my head for a while trying to figure out where this was coming from. Solution was to add jsonCompat=new to the query string. Listed in the docs here: https://developer.yahoo.com/yql/guide/response.html#json-to-json-transformation

how read HTTP GET variables in javascripts? [duplicate]

This question already has answers here:
How do I get query string value from script path?
(5 answers)
Closed 8 years ago.
I wanna know is this possible I pass get parameters for a javascript file and use them in codes?
I have this in html:
<script type="text/javascript" src="/javafile.js?q=somtext"></script>
know how I use this "q" parameter in my script codes? is this possible?
You can either:
Generate the JS dynamically with a server side language and read the query string there
Assume that the JS is the last <script> element so far in the DOM and parse document.scripts[document.scripts.length - 1].src
Note that if the script element has been added dynamically (i.e. with JS), it might not be the last script in the DOM.
I think Quentin's suggestion is the answer to your question.
I usually use an alternative way for this, which may also help you:
Make sure that your javascript is written in a library form, and make sure that you have an instantiate method/function inside your javascript that allows you to pass parameters (or better, as an object)
// on dom load:
library.init({ var1: value1, var2: value2});
This allows you also to load your javascript in a different way, and allows for cleaner code.
Or you can use option 3: use a library that has this functionality, e.g. MooTools: http://mootools.net/docs/more/Types/String.QueryString

Categories

Resources