Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
smathis
Posts: 21
Registered: ‎01-17-2012
My Device: BlackBerry 9550
My Carrier: ???
Accepted Solution

Browserfield XMLHttpRequest not valid

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&colon;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.

Please use plain text.
New Contributor
KoosMos
Posts: 4
Registered: ‎01-27-2012
My Device: Developer
My Carrier: Orange

Re: Browserfield XMLHttpRequest not valid

I have the same problem.

Have you find a solution ?

Please use plain text.
Contributor
smathis
Posts: 21
Registered: ‎01-17-2012
My Device: BlackBerry 9550
My Carrier: ???

Re: Browserfield XMLHttpRequest not valid

[ Edited ]

I find the solution.

What is wrong is to use an absolute url to open the XMLHttpRequest. It needs to use a relativie url (demo_get.asp) and the browserfield needs to have the baseurl set correctly (http://www.w3schools.com/ajax/).

That's very inconvenient.

Please use plain text.
New Contributor
KoosMos
Posts: 4
Registered: ‎01-27-2012
My Device: Developer
My Carrier: Orange

Re: Browserfield XMLHttpRequest not valid

Ty bro.

The other f****g developpers just says "I solve the problem".

You, you give the solution, thanks.

Please use plain text.