1

I'm getting the following error in the console when I am trying to return a string of JSON from a AJAX call.

Uncaught SyntaxError: Unexpected token

I have added a codepen with the code I am using.

If you click the convert button and look in the console you will see the error.

I cannot for the life of me figure it out.

Thanks.

JB

http://codepen.io/anon/pen/XJvyWq

jQuery('.convert').click(function(){
    jQuery.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback=?", function(data){
        console.log("Data: " + data);
    }, 'json')
});
Fares M.
  • 1,538
  • 1
  • 17
  • 18
johnnyxbell
  • 27
  • 1
  • 8
  • 3
    You dont need to add `’json’` at the end of the get request. – darkknight Apr 15 '15 at 23:26
  • If I don't I get the following error: XMLHttpRequest cannot load https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback=?. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dev.responsive.sportsgirl.com.au' is therefore not allowed access. – johnnyxbell Apr 15 '15 at 23:28
  • 1
    Here is the codepen link `http://codepen.io/anon/pen/XJvyWq` – darkknight Apr 15 '15 at 23:30

4 Answers4

2

First remove the "=" .

for a string display in the console you can use the methode stringify of the JSON object

jQuery('.convert').click(function(){


jQuery.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback", function(data){
         console.log("Data: " +JSON.stringify(data) );
            }, 'json');
        });

or for display an object in the console you can juste write :

jQuery('.convert').click(function(){

jQuery.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback", function(data){
         console.log(data );
            }, 'json');
        });

the result :

Object {To: "CAD", From: "EUR", Rate: "1.3138"}
Bouraoui KACEM
  • 821
  • 1
  • 8
  • 20
0

Remove the callback=? from the end of the URL you are providing OR remove the 'json' from the .get() method call.

jQuery('.convert').click(function(){
  jQuery.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD", function(data){
        console.log(data);
  });});

This is because jQuery assumes that when you pass a "callback" parameter in URL and type 'json' that the response will be JSONP. But in this case the server sends a JSON string. Instead of passing "?" if you pass the name of an existing function the error will not be thrown.

The current response by the server is:

{"To":"CAD","From":"EUR","Rate":"1.3138"}

If the Server was responding with JSONP and if the callback you passed was "test" then the response would have been:

test({"To":"CAD","From":"EUR","Rate":"1.3138"})

http://codepen.io/tejzpr/pen/yymQNz?editors=101 explains both cases.

Community
  • 1
  • 1
tejzpr
  • 945
  • 8
  • 19
0

Remove json at the second parameter as @SnehalShah said and it'll work

$('.convert').click(function(){
    $.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback=?", function(data){
         console.log("Data: " + JSON.stringify(data));  
    });
});
Fares M.
  • 1,538
  • 1
  • 17
  • 18
0

http://jsfiddle.net/1z20m3pL/

Try this way::

jQuery('.convert').click(function(){
    jQuery.get("https://rate-exchange.herokuapp.com/fetchRate?from=EUR&to=CAD&callback=?", function(data){
                    console.log("To: " + data.To);
                    console.log("From: " + data.From);
                    console.log("Rate: " + data.Rate);
                });
            });
kscius
  • 376
  • 9
  • 13