Nishan Hitang
Nishan Hitang

Reputation: 805

Uncaught SyntaxError: Unexpected token : for a valid JSON

I am trying to get a cross-domain JSON data via $.ajax method

$.ajax({
      type: "GET",
      dataType: 'jsonp',
      url: "http://nrb.org.np/exportForexJSON.php?YY=2016&MM=06",
      crossDomain : true,
    })
    .done(function( data ) {
        console.log("done");
        console.log(data);
    })
    .fail( function(xhr, textStatus, errorThrown) {
      console.log(xhr);
      console.log(xhr.responseText);
        // alert(xhr.responseText);
        // alert(textStatus);
    });

The JSON returned by the url is

{
    "Conversion": {
        "Currency": [{
            "Date": "2016-06-23",
            "BaseCurrency": "INR",
            "TargetCurrency": "NPR",
            "BaseValue": "100",
            "TargetBuy": "160.00",
            "TargetSell": "160.15"
        }, {
            "Date": "2016-06-23",
            "BaseCurrency": "USD",
            "TargetCurrency": "NPR",
            "BaseValue": "1",
            "TargetBuy": "107.76",
            "TargetSell": "108.36"
        }, {
            "Date": "2016-06-23",
            "BaseCurrency": "BHD",
            "TargetCurrency": "NPR",
            "BaseValue": "1",
            "TargetBuy": "285.75",
            "TargetSell": "N/A"
        }]
    }
}

I checked if the JSON is valid by using http://jsonlint.com/. The JSON is Okay. I get a console error.

Uncaught SyntaxError: Unexpected token :

The console is pointing to the error in the following screenshot

enter image description here

Upvotes: 3

Views: 1747

Answers (2)

4dgaurav
4dgaurav

Reputation: 11506

Responses with javascript content-type are evaluated automatically. JavaScript JSON parser treats { and } as a block instead of object.

Set correct Content-Type header for the JSON file. OR save it with .json extension.

Your url has .json extension while header is jsonp , Try making it same

Not quite sure of POST and JSONP : Post data to JsonP

Upvotes: 0

guest271314
guest271314

Reputation: 1

Try changing dataType to "json"

Upvotes: 1

Related Questions