5

What I'm trying to do

I'm programming a currency converter, and to not have to manually update the current currency, I get the current value from another website trough AJAX and Whatever Origin (to allow access to another domain). I tested it in a separated page and it worked perfectly, i.e. showed the current currency. However, when I inserted it in the actual code of the converter ...

The error

... any console accuses illegal character inside the jQuery file, even if I link to Google's library:

SyntaxError: illegal character            jquery.min.js:1:4  
ReferenceError: $ is not defined          Converter.html:75:0

Wherever I put it (in the beginning, middle or end), the same error happens, but only if I insert my code there, if I only link the jQuery file, no errors are showed.

The code

$.getJSON('http://whateverorigin.org/get?url=' + 
    encodeURIComponent('http://usd.fx-exchange.com/brl/') + '&callback=?',
    function (data) {
        currency = $('.today_s', data.contents).html();
        currency = currency.match(/\d\.\d\d\d\d/);
});

The page I'm trying to move to: here.

The working test page: here.


I don't even have a clue of what is happening..

Yuuza
  • 179
  • 9

2 Answers2

2

After many tweaks, finally I got rid of that error! What I did:

First I moved the contents of the actual page to the test page. Then I moved my script to a separated .js file. Then the error accused "illegal character" for arithmetic symbols (/ and *) in the functions in the beginning of the file. So I moved them to the end. Then I moved the jQuery code to the beginning of the .js file. Then finally I got free! =D

I don't know what was the real error, the only things I know is that it wasn't an "illegal character", and doing what I did fixed that.

By the way, thanks for the attention to who tried to help, even though.

Yuuza
  • 179
  • 9
0

Verify your js link and you should run your jQuery code once jquery is loaded:

$(document).ready( function() {

 $.getJSON('http://whateverorigin.org/get?url=' + 
encodeURIComponent('http://usd.fx-exchange.com/brl/') + '&callback=?',
function (data) {
    currency = $('.today_s', data.contents).html();
    currency = currency.match(/\d\.\d\d\d\d/);
   });
});
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • That's not how the `ready` event is used. It's used to run code once the document is loaded, not once jQuery is loaded. – Guffa May 31 '15 at 21:15
  • Ok, I updated now and the error insn't in the jQuery file anymore. Now the error is in another part of the code `SyntaxError: illegal character get:1:4`. Firebug gives more details: `get?url=http%3A%2F%2Fusd.fx-exchange.com%2Fbrl%2F&callback=jQuery111300854241960113451_1433108296988&_=1433108296989 (line 1, col 4)` – Yuuza May 31 '15 at 21:40
  • Isn't there a way to get the currency from the test page and store it in a variable in the actual page? Nothing works, only the test page works, I don't know why.. – Yuuza May 31 '15 at 21:55