用jquery接收json?

时间:2012-10-01 13:37:34

标签: jquery ajax json api jsonp

有人可以帮我用jquery接收这个json吗?

{
"output_type":"json","diagnostic":{"status":"200","elapsetime":"0.4187","memoryusage":"17.91MB","confirm":"success","lang":"id","currency":"IDR"},"results":{"result":[{"id":"business:3772","label":"Puri Maharani Boutique Hotel & Spa - Denpasar, Denpasar Timur, Kesiman Kertalangu","value":"Puri Maharani Boutique Hotel & Spa","category":"Hotel"},{"id":"business:3852","label":"Maharani Hotel  - Jakarta Selatan","value":"Maharani Hotel ","category":"Hotel"},{"id":"business:3874","label":"Maharadja Hotel - Jakarta Selatan, Mampang Prapatan, Tegal Parang","value":"Maharadja Hotel","category":"Hotel"},{"id":"business:4776","label":"Pita Maha Resort and Spa - Gianyar, Ubud, Ubud","value":"Pita Maha Resort and Spa","category":"Hotel"},{"id":"business:4779","label":"The Royal Pita Maha Resort and Spa - Gianyar, Ubud, Kedewatan","value":"The Royal Pita Maha Resort and Spa","category":"Hotel"},{"id":"business:18420","label":"Villa Mahapala Bali - Denpasar, Denpasar Selatan, Sanur","value":"Villa Mahapala Bali","category":"Hotel"},{"id":"business:105246","label":"Mahagiri Dreamland Villas & Spa - Badung, Kuta Selatan, Pecatu","value":"Mahagiri Dreamland Villas & Spa","category":"Hotel"}]},"token":"bc8d3a106572e4cff038261afb568ecf"}

我使用$ .getJSON和$ .ajax函数但遇到了一些问题,有人可以用一些jquery代码帮助我吗?

这是我的代码,

function getAPI() {
            jQuery.ajax({
            url: 'http://api.master18.tiket.com/search/autocomplete/hotel?q=mah&token=90d2fad44172390b11527557e6250e50&secretkey=83e2f0484edbd2ad6fc9888c1e30ea44&output=json',
        beforeSend: function(x) {
            if(x && x.overrideMimeType) {
             x.overrideMimeType("application/j-son;charset=UTF-8");
            }
        },
            type:"get",
            dataType: 'json',
            crossDomain: true,
         success: function(data) { console.log(data); }
         });
    }

但错误是Uncaught SyntaxError:意外的令牌:

1 个答案:

答案 0 :(得分:2)

您的问题似乎标有jsonp,因此我怀疑您正在尝试执行跨域AJAX调用。如果是这种情况,如果远程服务器支持它,您确实可以使用JSONP

因此,如果您没有进行跨域AJAX调用:

$.getJSON('/script', function(json) {
    // TODO: manipulate the result here
});

如果远程服务器支持JSONP:

$.getJSON('http://example.com/script?callback?', function(json) {
    // TODO: manipulate the result here
});