2016-03-15 57 views
0

我從我的供應商返回JSON數據之一的API,API是如下: - http://shipdesk.in/mobile/mobile_quick.php?country=IN&to_country=IN&pin=560001&weight=2.5&unit=KG&need_cod=No&to_pin=700001獲取JSONP數據

現在這個API不具備JSONP編碼,所以我不能附加&callback=processJSON這個。

我讀過Yahoo Api可以將其從JSON轉換爲JSONP,我可以在我的網站中使用它,但是我沒有成功。

另外我的網站有幾個限制,所以我不能爲此編寫php代碼。它必須在Javascript中完成。

的JSONP代碼(另一個示例),其工作原理是如下: -

<span id='test'>nothing</span> 

<script> 
    function processJSON(json) { 
     // Do something with the JSON response 

     result = JSON.stringify(json); 

     //alert(result); 
     document.getElementById("test").innerHTML = result; 

    }; 
</script> 

<script src='http://api.flickr.com/services/feeds/photos_public.gne?tags=monkey&tagmode=any&format=json&jsoncallback=processJSON'></script> 

任何想法,鏈接,教程和代碼段將非常感謝。

回答

0

可能這是你的預期。 使用雅虎API正常JSON轉換爲JSONP類型(yahoo link):

<span id='test'>nothing</span> 

<script> 
/* function processJSON (json) { 
    // Do something with the JSON response 

result=JSON.stringify(json); 

//alert(result); 
document.getElementById("test").innerHTML = result; 

}; 
*/ 
</script> 

<script> 
var yql_url = 'https://query.yahooapis.com/v1/public/yql'; 
var url = 'http://shipdesk.in/mobile/mobile_quick.php?country=IN&to_country=IN&pin=560001&weight=2.5&unit=KG&need_cod=No&to_pin=700001'; 

$.ajax({ 
    'url': yql_url, 
    'data': { 
    'q': 'SELECT * FROM json WHERE url="'+url+'"', 
    'format': 'json', 
    'jsonCompat': 'new', 
    }, 
    'dataType': 'jsonp', 
    'success': function(response) { 
    console.log(response); 
    document.getElementById("test").innerHTML = JSON.stringify(response); 
    }, 
    'error': function(error) { 
    document.getElementById("test").innerHTML = "error"; 
    } 
}); 
</script> 

新的腳本有使用我們的網址Ajax調用API雅虎。 但是,您應該使用您的API響應數據來信任雅虎。

希望這會有所幫助。

+0

感謝億,它就像一個魅力。而且我相信雅虎用於我的API:P –

+0

有趣的是,它使用to_pin = 700001,但與to_pin = 400001不兼容。是否有可以訪問多少個API的限制? –