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

Native Development

Reply
Developer
sanj1504
Posts: 109
Registered: ‎06-25-2013
My Device: Z10

AJAX call from javascript

Hi,

 

  Is ajax call possible from javascript ? I am using it this way in my javascript function.

 

$.ajax({
	url: ipAdd,
	type: "POST",
	cache: false,
	data: jsonString,
	dataType: 'json',
	success: function(json) {
		// some processing
	},
	error: function() {
	// some processing
	},
	async: false
});

 

I use webview to display html pages(contain js) where i need to call my server using ajax.

 

 

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: AJAX call from javascript

[ Edited ]

Hi,

 

I've done it, works nicely...

 

(Editted to make it look like a read the whole post first time)

 

Thanks! 

 

 

 

--
Rob is no longer associated with BlackBerry.
Developer
sanj1504
Posts: 109
Registered: ‎06-25-2013
My Device: Z10

Re: AJAX call from javascript

Do I need to do anything else with it like enabling XHR for cross-domain calls, because my request is not reaching the server.

When I call this Ajax script, control simply goes to error function and executes it, all within a fraction of second without waiting for response for server. 

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: AJAX call from javascript

[ Edited ]

Hi,

 

Are you using remote html hitting the remote server, or are you using asset based html, and hitting a remote server?

 

If so - the server needs to respond with headers saying it permits the cross domain scripting.

 

I've got a webserver running on my laptop, and am hitting it from my Z10 over USB.

 

Nothing special to see in the asset html; (169.254.0.2 is the laptop)

<html>
<head>
<script src="jquery-2.0.3.min.js"></script>
<script>
function addText(message) {
	var fred = document.getElementById("fred");
	fred.innerHTML += message;
	fred.innerHTML += "<br />";
}
function testAjax() {
var settings = {
	error: function(a) { addText("Error: " + JSON.stringify(a)); },
	success: function(a) { addText("Worked: " + JSON.stringify(a)); },
	complete: function(a) { addText("Complete: " + JSON.stringify(a)); },
	url: "http://169.254.0.2:8080/hmm.jsp"
};
$.ajax(settings);
}
</script>
<body>
<input type="button" onclick="testAjax();" value="Click Me!"/>
<div id="fred" />
</body>
</html>

 

Magic is done in the server with headers: (JSP syntax - any server will have a way to specify headers...)

<%

String name = "Access-Control-Allow-Origin";
String allow = "*";

String origin = request.getHeader("Origin");
if ("local://".equals(origin)) {
	allow = origin;
}

response.setHeader(name,allow);

%>
<html>
<body>
This is some really cool data.
</body>
</html>

 Try testing on your desktop with a browser, i tested with Chrome. Access the html right out of your assets folder (NOT over http)...

 

I did find that chrome sends no origin, and requires *, WebView in Cascades sends local:// - and needs local:// in the response.

 

You may want to tweak your server responses to prevent people stealing your content...

 

Great reading:

https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS

 

--
Rob is no longer associated with BlackBerry.
Developer
Pumano-
Posts: 215
Registered: ‎11-05-2011
My Device: Z10

Re: AJAX call from javascript

If I want to get web content using Ajax I use cascades for it (that Ajax in QML don't have whitelisting) it's good for parsing something from any website without changes at server-side.

Server-side whitelisting can contain changes or security vulnerabilities, we need don't forget about it.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: AJAX call from javascript

Lots of ways to handle this...

 

You could also run a proxy in C/C++ - have your webview conect to the net via it - prune/insert headers as you need...

--
Rob is no longer associated with BlackBerry.
Developer
sanj1504
Posts: 109
Registered: ‎06-25-2013
My Device: Z10

Re: AJAX call from javascript

[ Edited ]

I have html pages and javascripts in asset folder.

 

Server is deployed on a different system and phone is connected via wifi to the server.

 

I tried your example in browser, it was working fine. Also on phone it worked well.

 

But one question it hits my server and response is also success but is always going to error function, i.e. error function is being called.

Developer
sanj1504
Posts: 109
Registered: ‎06-25-2013
My Device: Z10

Re: AJAX call from javascript

In my javascript i had these lines earlier to ajax call

 

 

$.ajaxSetup({
    type: 'POST',
    headers: {
        'cache-control': 'no-cache'
    }
});

 

So the call was not going to the server. When i removed these it went through and hit my server but with same issue "always executing the error function"  even if it is successful.

Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: AJAX call from javascript

[ Edited ]

What is the error you're getting? Can you share more code + URI?

--
Rob is no longer associated with BlackBerry.
Developer
sanj1504
Posts: 109
Registered: ‎06-25-2013
My Device: Z10

Re: AJAX call from javascript

[ Edited ]

I use asset based html, and hitting a remote server.

 

So, it seems a cross domain issue..  I am using simple html pages in my app.

 

my code and error is follows

 

function serverCall (jsonObject) {
	var serverip = jsonObject.serverip;
	// server ip is coming from jsonObject from the calling function, it is valid
	var jsonString = jsonObject.json;
	// json string which is coming is also validated 
	
   $.ajax({
        url: serverip,
        type: "POST",
        cache: false,
        data&colon; jsonString,
        dataType: 'json',
        success: function(json) {
            alert("server call successfull");
        	callServer_CB_Success(json);
        },
        error: function(json) {
        	alert(JSON.stringify(json));	
// throws this alert {"readyState":4,"status":404,"statusText":"error"} localStorage.setItem("alertTitle", "Failure"); localStorage.setItem("alertMsg", "server error"); alert('server error');
// after executing this line alert "server error" it dosenot go to calling function instead it stays here return "error"; }, async: false }); }

 

So for cross domain issue i need to tweak my server or my app ??

 

Or is it possible from applicaion side(changes in javascript or html).