Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-25-2012, 10:01 AM   PM User | #1
palsbo
New to the CF scene

 
Join Date: Feb 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
palsbo is an unknown quantity at this point
Problem with web-hop/redirect in http request

I have built a microcontroller, that collects various information from my house.

The build-in webserver returns this information in a JSON-string when requested via port 84. It all works nicely!

My website makes a http request to the microcontroller to get data and present it on a html page.

The html page with the javascript looks like this (simplified):


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<script type="text/javascript">
var myJson = "http://mycontroller.com:84/fields.json";
window.onload = getJSON();
function getJSON() {
var request = new XMLHttpRequest();
request.open("GET", myJson, true);
request.send(null);
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200) {
document.getElementById('st').innerHTML = "Data received</br>"+request.responseText;
} else if (failure) {
document.getElementById('st').innerHTML = "Request failed - " + request.status + ": " + request.statusText;
}
}
}
}
</script>
</head>
<body>
<div id="st"></div>
</body>
</html>



Yes – there is a cross-domain problem – but this has been solved and is outside this question. I have left this out of the code to simplify.

Now to my problem:

The code works without problem. As it can be seen from the url, I use port 84 on the microcontroller. I wanted to hide this, and created a web-hop on my DDNS (dyndns.com). This will redirect

'http://mycontroller.com:84' to 'http://mywebhob.dyndns.org'

I can test it by putting the two urls ( + '/fields.json') into my browser and in both cases, I get the json data back.

The two url’s are:

'http://mycontroller.com:84/fields.json' (same as in the code) and
'http://mywebhop.dyndns.org/fields.json'

HOWEVER!

If I change the value in the code of 'var myJson =' from the old url to the new url, the http-request return an ok response but no data????
The problem is caused by using a web-hop that redirects from a url with port to an url without a port – but why?????

Hoping for a smart answer (and maybe even a solution!)

Regards
Erik
palsbo is offline   Reply With Quote
Old 09-25-2012, 01:57 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
One thing (which is clearly not related to the problem but still wrong)
Code:
window.onload = getJSON;
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
httprequest json

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:21 AM.


Advertisement
Log in to turn off these ads.