3

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nishan Hitang
  • 855
  • 3
  • 10
  • 36
  • 1
    I don't think we can use POST with `jsonp`. Just use GET. – Shaunak D Jun 23 '16 at 05:57
  • you can't use that api with js, but you can use YQL to turn the JSON into JSONP – dandavis Jun 23 '16 at 06:01
  • @dandavis If I dont use jsonp, it wouldn't be a cross-domain request. It would throw this error. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. – Nishan Hitang Jun 23 '16 at 06:04
  • **[this answer](http://stackoverflow.com/a/21506127/3639582)** might help. – Shaunak D Jun 23 '16 at 06:06
  • JSONP needs to be implimented by the server, just like CORS. i'm afraid you need to proxy or use YQL if you want your users to be able to grab that data – dandavis Jun 23 '16 at 06:07
  • If you are already getting a JSON in response, I'd suggest you read this : http://stackoverflow.com/a/13022566/3437508 – hkm93 Jun 23 '16 at 06:09
  • JSON.stringify(data) and console that..you will not get that error – Naga Sai A Jun 23 '16 at 06:11
  • JSON.Parse(data) will throw that unexpected token : error and if you JSON.stringify , it will covert that value to JSON string – Naga Sai A Jun 23 '16 at 06:15
  • http://codepen.io/nagasai/pen/GqNwBG – Naga Sai A Jun 23 '16 at 06:15

2 Answers2

1

Try changing dataType to "json"

guest271314
  • 1
  • 15
  • 104
  • 177
0

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

Community
  • 1
  • 1
4dgaurav
  • 11,360
  • 4
  • 32
  • 59