XMLHttpRequest ?

  • Ads

XMLHttpRequest ?

Postby Daman » Tue Jun 15, 2010 4:09 pm

Hi,

I tried to use your service with the following XMLHttpRequest because I have no way to enable allow_url_fopen on my webspace. At the moment I use the curl-class to get it to work.

But maybe you know whats wrong and why I get the -> readystate: 4 | request status: 0 Error ?

URL to check -> http://www.elbglueck.de/geo.php

I hope you can help, kind regards from germany.

My Code:

[code]
<script type="text/javascript">

var request = false;

function geodata() {

// create Request
if (window.XMLHttpRequest) {
request = new XMLHttpRequest(); // Mozilla, Safari, Opera
} else if (window.ActiveXObject) {
try {
request = new ActiveXObject('Msxml2.XMLHTTP'); // IE 5
} catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP'); // IE 6
} catch (e) {}
}
}
// check Request
if (!request) {
alert("cannot create XMLHTTP");
return false;
} else {
// open Request
request.open('post', "http://ipinfodb.com/ip_query.php", true);
// send Header
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// send Request
request.send();
// Result
request.onreadystatechange = interpretRequest;
}
}

// check Result
function interpretRequest() {
switch (request.readyState) {
// readyState 4 and request.status 200 -> works
case 4:
if (request.status != 200) {
alert("readystate: "+request.readyState+" | request status: "+request.status);
} else {
// Server Response -> XML-Document
var xmlDoc = request.responseXML;

if (xmlDoc == null) { alert("wrong data"); break; }

// get Data
ip = xmlDoc.getElementsByTagName('Ip')[0].firstChild.nodeValue;
city = xmlDoc.getElementsByTagName('City')[0].firstChild.nodeValue;
//
alert("city: "+city);
}
break;
default:
break;
}
}
//-->

</script>
<?php

echo '<script type="text/javascript">geodata();</script>';

?>
[/code]
Daman
 
Posts: 1
Joined: Tue Jun 15, 2010 3:45 pm

Re: XMLHttpRequest ?

Postby marchost » Thu Jun 24, 2010 1:35 pm

Hi sorry for my late answer I was away for some time.

I think it's because you cant make Ajax request to other domains than your own...

There is a way around on this page : http://ipinfodb.com/ip_location_api_json.php

Marc
marchost
Site Admin
 
Posts: 373
Joined: Wed Apr 08, 2009 9:55 pm

Re: XMLHttpRequest ?

Postby guttmann » Sat Aug 28, 2010 3:48 am

Hi,

I am interested in using your service through JavaScript. My question is: Is there a way for you to set up your servers to allow cross domain ajax requests. To allow this functionality is just adding two additional header fileds in your php code.
I would very appreciate your kindness.
I created a live example: http://bsofts.com/crossdomainajax.html, check source
Thank you in advance.
Krisztian Guttmann
guttmann
 
Posts: 2
Joined: Sat Aug 28, 2010 3:27 am

Re: XMLHttpRequest ?

Postby marchost » Thu Sep 02, 2010 7:39 am

Hi, from what I know it's browsers that dont allow regular cross domain ajax. Am I wrong?

Did you try the script on this page : http://ipinfodb.com/ip_location_api_json.php (jquery version or not)
marchost
Site Admin
 
Posts: 373
Joined: Wed Apr 08, 2009 9:55 pm

Re: XMLHttpRequest ?

Postby guttmann » Wed Sep 08, 2010 4:19 am

Hi,

I tried the provided example on the page. It works. There is no problem with the given JavaScript API. I can imagine the provided code as a fallback solution if cross domain ajax request is not available. Cross domain ajax is available in modern browsers. It depends on two things:
1st: browser support (firefox, safari and chrome do support it)
2nd: you have to enable it on server side so as the browser know that you really wish to enable this functionality.

It can be enabled by your PHP code by sending these additional headers with each response.
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET

Further information:
https://developer.mozilla.org/en/HTTP_access_control
guttmann
 
Posts: 2
Joined: Sat Aug 28, 2010 3:27 am


Return to IP geolocation API related



Who is online

Users browsing this forum: No registered users and 1 guest

cron