I'm trying to launch an ajax request from javascript inside of a BrowserField.
Here is a demo application, it's just an html element which tries to make an ajax request on click.
public MyScreen() {
// Set the displayed title of the screen
setTitle("MyTitle");
BrowserFieldConfig _myBrowserFieldConfig = new BrowserFieldConfig();
_myBrowserFieldConfig.setProperty(
BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
_myBrowserFieldConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR,
Boolean.TRUE);
BrowserField browser = new BrowserField(_myBrowserFieldConfig);
browser.displayContent(
"<!DOCTYPE html><html><head>"
+ "<script>function loadXMLDoc(){"
+ "alert('t'); "
+ "var xmlhttp;"
+ "if (window.XMLHttpRequest) "
+"{ "
+ "xmlhttp = new XMLHttpRequest(); "
+ "} "
+ "else { "
+ "xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\"); "
+ "}"
+ " xmlhttp.onreadystatechange=function() "
+"{ "
+ " if (xmlhttp.readyState==4 && xmlhttp.status==200) "
+ "{"
// + " document.getElementById(\"myDiv\").innerHTML=xmlhttp.responseText;"
+ "}"
+"alert('State:'+xmlhttp.readyState+ 'status'+ xmlhttp.status)"
+ "} "
+ "xmlhttp.open(\"GET\",\"http://www.w3schools.com/ajax/demo_get.asp\",true);"
+ "xmlhttp.send(); "
+ "}"
+ "</script>"
+ "</head><body><h2>AJAX</h2><a onclick=\"javascript:loadXMLDoc();\">Request data</a><div id=\"myDiv\"></div></body></html>",
"http://www.w3schools.com");
add(browser);
}
The problem is that even the first alert is not working, so there should be a javascript syntax error, but well, everything looks good to me. I tried this code from Firefox on my computer and the alerts are shown (I know cross-domain isn't always possible). So, what is the error in my script ?
I have tested this on the simulator with BB 5.0.0 and 7.0.0.