07-11-2011 04:57 PM
Hi there. I am new to webworks and blackberry development. I tried searching for my answer but could not find anything. Maybe someone can help me as this probably is an easy question.
I wish to get the contents of an xml file from a url and display the contents in a nice format. I have tried using the following 2 js functions to no avail:
function loadMyXML() {
alert('hi...start!');
var xhr_object = new XMLHttpRequest;
xhr_object.open("GET", "****.ca/aa/aa/aa/maag.xml", false);
xhr_object.send(null);
alert('done');
}function loadXML(url) {
alert('hi!');
if (document.implementation && document.implementation.createDocument) {
var xmldoc = document.implementation.createDocument("", "", null);
//xmldoc.onload = function() { handler(xmldoc, url); }
xmldoc.load(url);
alert('im done!');
}
else
alert('it failed!');
}
I call them but they just dont complete (dont get the alert messages placed at the end).
Can someone tell me how I can extract the XML from an external URL? Thanks in advance!
07-12-2011 01:46 AM - edited 07-12-2011 01:46 AM
Looking forward to the response. I have had a similar question posted for weeks with no answer using jquery get. It appears that off domain requests are just ignored. Not sure if we need a different certificate, proxy or what.
07-12-2011 10:05 AM
Any ideas? ![]()
07-12-2011 10:15 AM
Hello, the external domain is white listed on your config.xml? (Permissions section) if not, add it (remember to check Apply access requests to subdomains).
And remember that XML requests are restricted by the "Same origin Policy" http://en.wikipedia.org/wiki/Same_origin_policy so if it doesn't work try JSONP (you wil need a JSONP aware web service)
Try using jquery library it will save you a lot of work.
07-12-2011 10:43 AM
Thanks for the reply kam!
Yes, I have this code snippet in my config.xml:
<access subdomains="true" uri="http://****/aaa/aaa/maag.xml"/>
But, you got me at the 'same origin policy'. Are you sure it would apply to such a case? I thought javascript could access any page.
How exactly will I go about using a jquery library in the webworks framework? (sorry I am new to this). I thought webworks was only html, js, css. Please correct me if I am wrong!
07-12-2011 11:03 AM
07-12-2011 11:19 AM
this is a JSONP ajax request, check jquery .ajax() documentation so you can play a litte more with the config:
//HTML jquery include
<script type="text/javascript" src="jquery/jquery-1.6.1.min.js"></script>
//javascript
function queryService(srvUrl, callbackFunc){
var timeout_json = 30; //seconds
//show page loading or wait message
//$.ajax
$.ajax({
type: "GET",
url: srvUrl,
crossDomain: true,
cache: false,
dataType: "jsonp",
jsonp : "callback", //Server only supports JSONP param name "callback"
jsonpCallback: callbackFunc,
timeout: timeout_json,
error: (function(xhr, ajaxOptions, thrownError){
//hide page loading or wait message
//show error message ie. "Status Code: ("+xhr.status+")<br>Description: "+xhr.statusText
}),
success: (function(data){
//hide page loading or wait message
//data may be an object or a simple string depends on the json format returned by the service
//you can use your response data here and also on you callback function.
})
});
}
//function usage
queryService("http://www.mysite.com/myservice.php","mycallbackfunc");
function mycallbackfunc(data){
//alert or something fancy.
}