[英]error in parsing json
以下是解析從api接收到的json文件的代碼。 但是解析之后不會顯示任何輸出,
var codingAPI = "http://glosbe.com/gapi/translate from=eng&dest=hin&format=json&phrase=boy";
$.getJSON(codingAPI, function (json) {
// Set the variables from the results array
var address = json.tuc[0].pharse.language;
$('#address').text(address);
});
// Caching the link jquery object
var $myLink = $('a.myLink');
// Set the links properties
$myLink.prop({
href: codingAPI,
title: 'Click on this link to open in a new window.'
}).click(function (e) {
e.preventDefault();
window.open(this.href, '_blank');
});
代碼放在jsfiddle中
這是由於相同的原產地政策。
查看控制台錯誤
XMLHttpRequest無法加載http://glosbe.com/gapi/translate?from=eng&dest=hin&format=json&phrase=boy 。 所請求的資源上沒有“ Access-Control-Allow-Origin”標頭。 因此,不允許訪問源“ http://fiddle.jshell.net ”。
由於不允許對此文件進行跨域請求,因此可以使用JSONP-服務器支持。 使用jQuery,您可以只使用?callback=?
使響應成功。
var codingAPI = "http://glosbe.com/gapi/translate?from=eng&dest=hin&format=json&phrase=boy&callback=?";
$.getJSON(codingAPI, function (json) {
// Set the variables from the results array
var address = json.tuc[0].phrase.language;
$('#address').text(address);
});
// Caching the link jquery object
var $myLink = $('a.myLink');
// Set the links properties
$myLink.prop({
href: codingAPI,
title: 'Click on this link to open in a new window.'
}).click(function (e) {
e.preventDefault();
window.open(this.href, '_blank');
});
另外,您在回調函數中pharse
了phrase
作為phrase
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.