JSON Reader issue: cannot access jsonData - javascript

I have a strange thing going on here: I am using a JSON reader within a store to fetch search results. After loading the store I receive data or error states (built together as a JSON, too). So in both ways I get a successfully response, so I have to check the JSON for myself to trap "error conditions". But I cannot access the jsonData property that shhould be a JSON object within the reader. Chrome tells me that:
I can access the applyDefaults though (it returns true in that case) but not the jsonData.
My code looks like this:
var result = searchStore.getProxy().getReader();
console.log(result.jsonData);
The output is "undefined". As you can see in the picture the jsonData object holds my JSON (with the isError property I wanted to access).
What I am doing wrong?

You need to think more async and think of the timing it takes for a request to return and when you are trying to get the jsonData. Instead of using console.log, set a breakpoint or use the debugger; statement so you can freeze the browser and walk through the code. You can then inspect variables and such to see what the object looks like.

Try to access your JSON data in a success callback, to make sure the data is gathered from the server.

Related

DataCloneError: An object could not be cloned

I need to pass a Hash function by using the javascript postMessage. I'm using the browser Firefox. When I'm sending this I'm getting the below exception.
'DataCloneError: An object could not be cloned' The code which I have implemented so far is as below.
var mes = CryptoJS.SHA256(clientId + origin + sessionState);
opIFrame.postMessage(mes,endPoint);
When I'm sending a string value this works successfully. I tried to send mes.toString() but it still gives the same error. I saw several threads on this topic. But could not configure a solution. Please advice me on this.
Ensure that the mes variable contains a WordArray object after the call to CryptoJS.SHA256 by logging it into the console. It looks like the object you're trying to send via postMessage() is not what you're expecting (possibly an Error or a Function object).

JS receiving enumMap in json see fields as undefined

I have REST ws which send json. Inside json there is enumMap (from java) and when I recieve json from REST, it looks like this:
{
map:{
KEY1:VALUE1,
KEY2:VALUE2}
}
when i try to do:
var something = map.KEY1;
console.log(something);
i see that something is undefined but when i do
console.log(map.KEY1);
than i see VALUE1
I don't see why it is not working. I tried many different set ups but in every one of them behaviour is the same or exception was thrown. Any ideas ?

Working with JSON via GET requests in Javascript

I've built a php API that provides data in json output, I need to get the values via a get request to then plot as a graph on the page.
The front end web component in hosted on the same server as in the api in this basic structure:
index.php
graph.php
/api/
/api/src
/api/src/api.php
My current code in graph.php is as follows:
<script>
var myJson;
$.getJson('api/src/api.php/poll/results/current/13/', function(jd){
myJson = jd.AnswerCount.1;
});
document.getElementById('jsonhere').innerHTML = myJson; //just to test
</script>
The endpoint outputs data like the following:
{"AnswerCount":{"1":5,"3":1,"2":2,"4":1,"5":5,"6":3,"7":2}}
Which I need loaded into a key-value pair array,
1:5
3:1
2:2
4:1
...
to then be put into the graphing library.
How do I fix my code/write new code to do this? I'm pretty stuck here.
EDIT:
On a hunch I logged all the get requests via wireshark, and no request is ever sent to the url in question. Even with an empty function { } ? http://grab.kfouwels.com/pmgW
You can't use a number as an identifier, to access the 1 property you have to say [1] not .1
You have to use the variable containing your data, not x which hasn't been mentioned until you try to assign it somewhere
The A in Ajax stands for Asynchronous. You have to work with your data inside your callback since the function you pass to getJson won't be called until the HTTP response arrived but the line starting document.get will run as soon as the HTTP request has been sent.

Json can not be parsed when received dynamically (using socket.io)

I am receiving json-datas which is 100% correct formatted json data.
My problem is,
when I run the following code it works:
var data = {"datas":[{"matts":{"active":"1","status":"off"},"config":null,"adapters":[]}}]};
console.dir(data); // it works.
but when I receive the same data on socket.io like following code, its not working:
_liveSock.on('sm', function(data)
{
console.log(data); // I am receiving the data correctly.
console.dir(data); // But I cant display the data.
});
Both codes are almost the same logic, only the second one is received dynamically.
When I try to use console.dir(data); on second code, it gives this error:
there is no kind object
(btw. I am using Firefox/Firebug)
Is there another method or way to use dynamically received json datas?
Thank you!
You can't use console.dir() until you have parsed the value.
console.dir(JSON.parse(data));
The JSON facility is not available in older browsers. You can find parsers online.

Accessing JSON values with a variable

I'm trying to access JSON data with jQuery and grab a specific set of values based on a variable. I've done this before using [] but for some reason I can't figure out what is going wrong this time.
My JSON file (being read in by getJSON, and named jsonmaker.php) looks like this:
{"0107001":{"label":"Canada","x":"0","y":"0.34"},"0107002":{"label":"USA","x":"-0.16","y":"0.53"}}
I then have a function which is essentially this:
function addAttrib(attrib) {
$.getJSON("jsonmaker.php", function(data) {
alert(data[attrib].label);
}
}
But it keeps returning undefined. Any idea what I'm doing wrong? I've checked to make sure the var going to attrib is 0107001, no problems there.
Also, I know my JSON file is a php file so I could filter what's returned to match the attrib value, but I'm looking to develop something that can run purely on HTML and JS, so I could just pack the JSON file for the project and take it with me. No need for a web server w/ PHP etc.
The data access itself works for me:
var data = {"0107001":{"label":"Canada","x":"0","y":"0.34"},"0107002":{"label":"USA","x":"-0.16","y":"0.53"}};
var attrib = "0107002";
alert(data[attrib].label); // USA
Make sure that attrib remains untouched between the moment you call addAttrib() and the moment when the AJAX request completes and your anonymous callback function gets called.
Update: is this your real code? You have at least one syntax error:
function addAttrib(attrib) {
$.getJSON("jsonmaker.php", function(data) {
alert(data[attrib].label);
}); // <- Please note missing ");"
}
In my experience, $.getJSON() doesn't always return an object. Depending on the MIME type that the server returns along with the JSON, you might end up with a string instead of an object. Check what data contains. If it's a string, you must manually parse it using eval() (old style) or JSON.parse() (new browsers only).
try to list all properties from data, to have sure the data is being returned:
for (var p in data){
if (data.hasOwnProperty(p){
alert(data[p]);
}
}
It's not your solution but with this you can know how your data is coming.

Categories

Resources