I'm a real noob in js. I've seen a lot of questions about this, but none of them seems got it clear to me. They all falls into json, that is not available. I'm trying to access a very poorly documented API(it is in portuguese...). It does not support Json nor Jsonp. It is only xml, via SOAP, HTTP GET e HTTP POST. And of course I'm getting:
XMLHttpRequest cannot load http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados. Origin http://kubrusly.com is not allowed by Access-Control-Allow-Origin.
my code is:
$.ajax({
type: "GET",
url: "http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados",
dataType: "xml",
success: parseXml
});
When working locally this goes fine, but once uploaded to my server, it won't retrieve nothing...
Curious enough if i try dataType: "jsonp"
:P i get an error complaining about unexpected char '<'. And the xml doc is visible in Safari's console. So the data, in xml format, is arriving in browser, it is there, but i can't access it...
So if json is not an option, how can i go to retrieve this data? It is so frustrating.
I pasted the testing code here:
@Kevin B Thanks for your answer. I did a little research on CORS. I got a little confused with that. Is this supposed to happend on the client side? Any way I got an .htaccess from html5 boilerplate, uncommented the
[EDIT] - solved.
Ok, so I get it going, thanks for the help of you guys. I'll let my solution registered here...
I found that CORS stuff is done in the server side, not my case. I did not try YQL, you know one more variable, syntax... I let this for last, but did not need it. I managed to set a proxy and go through it, i spend a lot of time before setting the user agent, and that did it. Before that I aways got 403 error...
this page from yahoo, helped my a lot, some good code examples, both js and PHP.
here the code that is working for me:
js:
// Based on script from yahoo's example @ http://developer.yahoo.com/javascript/samples/ajax/sample_proxy_ajax.html
// tweaked by vk
// end point without domain...
var endPtpath = '/SitCamaraWS/Deputados.asmx/ObterDeputados?';
// PHP proxy
var proxiedUrl = 'http://kubrusly.com/yxd/php_proxy_simple.php?yws_path=' + encodeURIComponent(endPtpath);
// mind browsers...
var xmlhttp = null;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
//overrideMimeType if supported
if ( typeof xmlhttp.overrideMimeType != 'undefined') { xmlhttp.overrideMimeType('text/xml'); }
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Perhaps your browser does not support xmlhttprequests?');
}
// Create an HTTP GET request
xmlhttp.open('GET', proxiedUrl, true);
// Set the callback function
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// Output the results
console.log(xmlhttp.responseText);
} else {
// waiting for the call to complete
console.log("waiting...");
}
};
// Make the actual request
xmlhttp.send(null);
PHP
<?php
// PHP Proxy --- only 'GET'
// Based on script from yahoo's example @ http://developer.yahoo.com/javascript/samples/proxy/php_proxy_simple.txt
// tweaked by vk
// hostname - just base domain
define ('HOSTNAME', 'http://www.camara.gov.br');
// Get the REST call path from the AJAX application - js;
$path = $_GET['yws_path'];
// assign yo url
$url = HOSTNAME.$path;
//Open the Curl session
$session = curl_init($url);
// Don't return HTTP headers. Do return the contents of the call
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPGET, true);
//set user agent, that did it!!! copied from browsers...
curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36');
// The web service returns XML. Set the Content-Type appropriately
header("Content-Type: text/xml");
//// verbose mode for debuging good tool!
// $fp_err = fopen('verbose_file.txt', 'ab+');
// fwrite($fp_err, date('Y-m-d H:i:s')."\n\n"); //add timestamp to theverbose log
// curl_setopt($session, CURLOPT_VERBOSE, 1);
// curl_setopt($session, CURLOPT_FAILONERROR, true);
// curl_setopt($session, CURLOPT_STDERR, $fp_err);
// Make the call
$xml = curl_exec($session);
// done, shutDown.
curl_close($session);
?>