简体   繁体   中英

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

在此输入图像描述

尝试将dataType更改为"json"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM