/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Trying to GET XML from other server (webservice)

Hi,

Well, what I’m trying to do is to get a xml from a webservice.
When the user types a key, buscarCEP() is called and if the field value is a valid address code (xxxxx-xxx), AJAX makes the request to the webservice, but always returns ajax.status == 0 on ajax.readyState == 4.
I found somewhere instruction to use ajax.overrideMimeType(“text/xml”) to specify the request as being for XML.

First, here’s the xml:
[url]http://www.buscarcep.com.br/?cep=60510-430&formato=xml&chave=12A4UYmXG2XVtk/yBh7AEMkc.4xk8y[/url].

Please, take a look at my code:

[CODE]function buscaCEP(input,enderecoArr){
var cep = input.value;
var cepRegEx = /^(d{5}-d{3})$/;
var regExp = new RegExp(cepRegEx);
if(regExp.test(cep)) //checks if the entered value is valid
{
var ajax = GetXmlHttpObject();
if(ajax)
{
if(ajax.overrideMimeType)
ajax.overrideMimeType(“text/xml”); //specifies the request for XML
ajax.onreadystatechange = function(){
if(ajax.readyState == 4)
{
if(ajax.status == 200)
{
setaEndereco(ajax.responseXML,enderecoArr);
}
else{
alert(ajax.status);
}
}
}
cep = encodeURIComponent(cep);
var metodo = “GET”;
var url = “http://www.buscarcep.com.br/?cep=”+cep+”&formato=xml&chave=12A4UYmXG2XVtk/yBh7AEMkc.4xk8y.”;
ajax.open(metodo,url,true);
ajax.send(null);
}
}
}

function GetXmlHttpObject(){
if(window.XMLHttpRequest){
return new XMLHttpRequest();
}else if(window.ActiveXObject){
return new ActiveXObject(“Microsoft.XMLHTTP”);
}
return null;
}[/CODE]

I’m out of ideas… is this happening because I’m trying to access a server that’s not my own?
Can anybody help me?

Thanks in advance,

Hênio

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@A1ien51Apr 03.2010 — JavaScript has a same domain policy, you can not make a call to another server. You need to use a proxy on your server to get the file and return it to your page.

Some browsers support CORS, but that means you have control of both ends.

If the other server supported JSONP, you could use that also.

Eric
Copy linkTweet thisAlerts:
@GraniteauthorApr 03.2010 — Ooooh yes!

I just created a php script, that will be called by ajax, to access that xml.

Actually the php code isn't working, but I think I've already figured out why. My server is blocking access to files out of itself, returning an error:
URL file-access is disabled in the server configuration in E:homenanettemWebscr.endereco.php on line 4
[/QUOTE]

I guess all I have to do now is ask my host to enable allow_url_fopen, in php.ini.

Well, thank you very much, A1ien!
Copy linkTweet thisAlerts:
@GraniteauthorApr 21.2010 — Ok, sorry for bringing this back to live, but something came to my mind.

If "JavaScript has a same domain policy", how is it that I can add javascript advertising codes (such as Google Adds) that access other server and then load something into my site?
Copy linkTweet thisAlerts:
@criterion9Apr 21.2010 — The javascript itself is loaded from the other domain.
Copy linkTweet thisAlerts:
@A1ien51Apr 22.2010 — Ok, sorry for bringing this back to live, but something came to my mind.

If "JavaScript has a same domain policy", how is it that I can add javascript advertising codes (such as Google Adds) that access other server and then load something into my site?[/QUOTE]


You can load resources [stylesheets, videos, JavaScript, images, etc] from other domains. That is how JSONP works.

Eric
Copy linkTweet thisAlerts:
@GraniteauthorApr 22.2010 — Oh yes, of course, the thing you had told me about in the other post, yeah?

The link you had posted was of a page about JSONP in Jquery. And since I'm still not a fan of Jquery (because atm I'm focusing on improving my skills in straight javascript/ajax), I ignored the information on it. Sorry for that. :o

Well, thanks for your support, mates! (specially to A1ien51, for your patience :p )

I promise not to ressurect this thread again!
×

Success!

Help @Granite spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...