XmlHttpRequest not working

Ask, and ye shall receive.
loginAlreadyTaken
Posts: 2
Joined: 16:34, 26 Jan 2016

XmlHttpRequest not working

Postby loginAlreadyTaken » 16:51, 26 Jan 2016

Hello there,

I'm trying to send my app's data to a web-server.
Therefore I'd like to use Ajax.
My simplest approach so far:
- Copy the helloworld-example
- Add javascript to make an xhr-call to a .php that simply returns a number and alerts me when it's done

When I open the page on my pc it works correctly.
But when I try on an android device (3 different devices so far)
the request always has state: 1 and status: 0.

I can open the .php directly within the app and it works as it should,
and I can open other websites that call the same .php asynchronously.
I simply can't do it from the app itself.

Please help me,
Best regards

alex
Posts: 92
Joined: 00:59, 19 Nov 2013

Re: XmlHttpRequest not working

Postby alex » 09:20, 27 Jan 2016

Hi!

You're probably retrieving a URL over HTTP, while our service is based on HTTPS, hence you'll get a cross-protocol violation error.
(we run HTTPS between Workbench and Viewer, to conform with industry needs)

This means you may either want to (1) change to a resource starting with https:// like in this example:

Code: Select all

var flickerAPI = "https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON(flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
  })
  .done(function(data) {
    $.each(data.items, function(i, item) {
      $("<img>").attr("src", item.media.m).appendTo("#images");
      if (i === 3) {return false;} //only the first three items
    });
  });




or (2): use the Cordova HTTP plugin, which allows you to natively retrieve this resource. The code is a bit different than the normal HTTP call, in order to invoke the plugin functionality:

Code: Select all

cordovaHTTP.get(
   "http://somewhere.com/some-path/some-doc", 
function(response)  // on success
{
app.status( "HTTP success " + response.status);
app.onMessageArrived(JSON.parse(response.data));
},
function(response)   // on error
{
console.log(JSON.stringify(response));
});


And yes, in order to check what's coming in, you'd might want to also see if it's actually a JSON file (more advanced checks needed in some cases):

Code: Select all

var json;
try
   {  // is the resource well-formed?
   json = JSON.parse(client.responseText);
   }
catch (ex)
   {   
   // Invalid JSON, notify of the failure...
   alert(‘Could not parse json, aborting..’);
   }
if (json)
   {
   // Ok, it seems to be valid JSON, proceed here with the code
   }

loginAlreadyTaken
Posts: 2
Joined: 16:34, 26 Jan 2016

Re: XmlHttpRequest not working

Postby loginAlreadyTaken » 10:10, 27 Jan 2016

Hello alex,

thanks a lot, now it works like a charm.
You really made my day.

alex
Posts: 92
Joined: 00:59, 19 Nov 2013

Re: XmlHttpRequest not working

Postby alex » 11:03, 27 Jan 2016

Glad to help, keep us posted :-)

axl


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 1 guest