开发者

failed to open stream: HTTP request failed!

I am requesting a webservice to pull lat/lng coordinates by using country names from a php file using this code:

$defaultcountry = $_REQUEST['country'];
$daurl = "http://ws.geonames.org/search?q=".$defaultcountry.'&rows=5';
$contents = file_get_contents($daurl);

$xml = new SimpleXMLElement($contents);
$lat = $xml->geoname->lat;
$lng = $xml->geoname->lng;

$coords = array('lat'=>"$lat", 'lng'=>"$lng");

$o = array('success'=>true, 'coords'=>$coords);  


echo json_encode($o);

i dont have issues calling this file directly from the browser. The problem is when this file is being called by ajax somewhere, like so:

ajaxManager.add('cacheQueue',{
                                    url: '../lib/getdefaultcoord.php',
                                    type:'POST',
                                    dataType:'json',
                                    data:{country:Country},
                                    success:function(json){
                                        alert(json.coords.lng);

                                    }

                                    })

thus, error comes up as stated 开发者_运维问答on my subject line. I have a similar scenario for requesting countrynames using a different webservice url (webservicex.net). That one from geonames.com does not seem to work when requests are made via ajax... How come?


Is this URL correct from the page you do the AJAX request?

url: '../lib/getdefaultcoord.php'


you should get firebug and examine the XHR from it.

I think or your url is wrong or some parameters are not passed like expected


im providing something u guys can play if you won't mind: requesting file with ajax:

$(document).ready(function(){ 


        $.post('getdefaultcoord.php', {country:"united arab emirates"}, function(data){
            alert(data.coords.lat);

        },'json');


    });

on getdefaultcoord.php:


$defaultcountry = $_REQUEST['country'];
$url = "http://ws.geonames.org/search?q=".$defaultcountry."&rows=5";
$contents = file_get_contents($url);

$xml = new SimpleXMLElement($contents);
$lat = $xml->geoname->lat;
$lng = $xml->geoname->lng;


$coords = array("lat"=>$lat,"lng"=>$lng);
$o = array('success'=>true, 'coords'=>$coords);

echo json_encode($o);

u can play around the getdefaultcoord.php to make sure that the requesting file is working properly with its country request and so goes with the getdefaultcoord.php, notice that the original error i posted on my subject line is the same using firebug...


I think in your ajax call you should try to spell the whole path out for the url. Instead of using a relative - "../lib/getdefaultcoord.php" try using "http://www.yourdomain.com/lib/getdefaultcoord.php". The ajax call could be confused by the relative reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜