Jump to content

Recommended Posts

Hi guys my JS is poor at best i have this

[Object { prob2=23.5}, Object { prob1=23.375}]

showing in my console.log it comes from a AJAX request from PHP i'm getting this out put by this

$.ajax({    url:"temps.php",  	dataType: "json",    success:function(data) {   console.log(data);    }

how can i use thi information like this???

$.ajax({    url:"temps.php",  	dataType: "json",    success:function(data) {   		document.getElementById("prob1").innerHTML = data.prob1;    }

Please help cos this dont work lol

 

Thanks

Link to post
Share on other sites

Any errors in the console?

 

do you have control over the backend? It seems like there's an extra layer of nesting. I would expect a flatter object being returned, something like

{  prop1: 23.375  prop2: 23.5}

do you have a live link to this page anywhere?

Edited by thescientist
Link to post
Share on other sites

 

Any errors in the console?

 

do you have control over the backend? It seems like there's an extra layer of nesting. I would expect a flatter object being returned, something like

{  prop1: 23.375  prop2: 23.5}

it is coming from here

#!/usr/bin/pythonimport sys, json, oslist = os.listdir("/sys/bus/w1/devices/")list.remove("w1_bus_master1")# we can also build lists, first start with an empty oneoutput = []for sensor in list:	# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.	tfile = open("/sys/bus/w1/devices/" + sensor + "/w1_slave")	# Read all of the text in the file.	text = tfile.read()	# Close the file now that the text has been read.	tfile.close()	# Split the text with new lines (n) and select the second line.	secondline = text.split("n")[1]	# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).	temperaturedata = secondline.split(" ")[9]	# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.	temperature = float(temperaturedata[2:])	# Put the decimal point in the right place and display it.	temperature = temperature / 1000	#print temperature	output.append({ sensor:temperature })# Send it to stdout (to PHP)out = json.dumps(output,separators=(',', ':'))print out

output for this is [{"prob2":23.75},{"prob1":23.625}]

Edited by Craig Hopson
Link to post
Share on other sites

What you have there is an array with two elements:

Object { prob2=23.5}

Object { prob1=23.375}

 

So to access the elements you would need to use data[0].prob2 and data[1].prob1

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...