Fetching json from two fully functioning URLS. One works fine, the other does not

71 views Asked by At

I am trying to fetch some data from a Json-object.

In order to try this out (I am still new to all this) I have tried to fetch data from two different butt fully fully functional URL:s.

When using the URL from the pebble tutorials it all goes well, but when I'm trying from an other site, It does not work at all. It is like the Ajax-statement in my code doesn't even being executed if i use the second URL.

Note: I am only using one URL at once.

Since that last URL contains my private key, I cannot post it here. Instead I can post the json object.

Here's some info about my simple project: I am using Cloudpebble as an editor and the project is of the type pebble.js

The functioning Json object:

{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"base":"stations","main":{"temp":294.2,"pressure":1023,"humidity":64,"temp_min":292.15,"temp_max":295.93},"visibility":10000,"wind":{"speed":6.2,"deg":70},"clouds":{"all":75},"dt":1439384430,"sys":{"type":1,"id":5089,"message":0.0189,"country":"GB","sunrise":1439354486,"sunset":1439407710},"id":2643743,"name":"London","cod":200}

The not so fully functioning Json object:

{ "LocationList":{ "noNamespaceSchemaLocation":"hafasRestLocation.xsd", "StopLocation":[{ "idx":"1", "name":"BlÄsut (Stockholm)", "id":"300109187", "lat":"59.287913", "lon":"18.089955", "dist":"1" },{ "idx":"2", "name":"Sandsborg (Stockholm)", "id":"300109186", "lat":"59.284830", "lon":"18.089631", "dist":"342" }] } }

My code:

var UI = require('ui');
var ajax = require('ajax');

var URL = '<THE URL>';

// Get data
ajax(
  {
    url: URL,
    type: 'json'
  },

  function(data) {
    // Success!
    console.log("Successfully fetched weather data!");

    // Show to user
    var card = new UI.Card({
    title:'It is',
    subtitle:'working fine'
  });
card.show();
},

function(error) {
// Failure!
console.log('Failed fetching weather data: ' + error);
// Show to user

var card = new UI.Card({
title:'Does not work',
subtitle:'at all'
});
card.show();
}
);
2

There are 2 answers

0
charlee On

Looks like you are trying to load data from another site.

Usually ajax is only allowed to load data from its own site(to be accurate, from its own "domain"). However if loading from another domain, a tech called CORS will be involved. There are several rules about CORS -- see document here, but basically, whether a URL can be accessed by ajax from another domain or not is decided by the server.

Probably the reason of your issue is, the 1st URL is located in the same domain as the page loading your script, or, if the 1st URL is in another domain, the server allows CORS access. And for the 2nd URL, the server does not allow CORS access.

0
Ilias Bennani On

Hello again and thank you for all your help!

It is indeed embarrassing for me, but the solution was very simple:

In order to call the API's, you need to add http:// in fromt of the URL. The API I was working with did not write the http// in their documentation, therefore nothing was obtained from their server.

Hopefully my mistake and the solution will help others facing the same problems when developing using cloudpebble.