Null response from XMLHttpRequest in OS X Javascript Widget
I want to develop a little os x widget which fetches data from an xml source and then displays the results. My problem is I never used javascript XMLHttpRequest before and I'm not sure what I'm doing wrong.
To start my project I want to create a simple script which returns the response header and alert it. I've tried lot's of different things I found on the web and I know it can't be that difficult.
But whatever I try I get a 'null' response. I even tried to get the content directly but with no luck.
I once heared that cross domain requests don't work but I'm not sure if this info is up to date. And if it's up to date then how can I make a request to a server using javascript when the javascript runs local, on my Mac in a widget ??
Here's my code, maybe someone can show me what I'm doing wrong.
<html>
<title>This is a test</title>
<body>
This is a simple test.
<p class="example"><a href="#" onclick="init();return false;">Try this.</a></p>
<script type="text/javascript">
function init()
{
xmlhttprequest = new XMLHttpRequest();
xmlhttprequest.onreadystatechange = function() {
if (xmlhttprequest.readyState == 4) {
alert(xmlhttprequest.getAllResponseHeaders());
}
}
var url = "http://stinfwww.informatik.uni-leipzig.de" ;
xmlhttprequest.open("GET", url, true);
xmlhttprequest.send(null);
}
开发者_运维问答 </script>
</body>
</html>
If anyone got any suggestion I would really appreciate this. Or if you have any good resources (esp. on Javascript Widget Development) on this topic please let me know.
Thanks indeed
Browsers restrict cross-domain requests for security purposes. So Javascript at http://localhost can’t retrieve data from a remote API
Also read this http://htmltimes.com/cross-domain-javascript-xmlhttprequest-restriction.php
Hope that help's
精彩评论