我有一个来自我的供应商的api,它返回JSON数据,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编码,所以我不能将&callback=processJSON
附加到它上面。
我读到雅虎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>
任何想法,链接,教程和代码片段将非常感谢。
发布于 2016-03-15 16:31:15
也许这就是您所期望的。使用Yahoo 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使用我们的网址。但你应该相信Yahoo会提供你的API响应数据。
希望这能有所帮助。
https://stackoverflow.com/questions/36004826
复制相似问题