我试图访问谷歌图书以一个ISBN码得到一本书的细节,我有很多的问题,它们是:编码一个JSONP <SCRIPT SRC =请求
A)我想组装一个脚本请求,例如将ISBN代码连接到URL中。我没有成功地做到这一点 - 我不知道为什么。
B)然后我想用这个生成的脚本动态更新DOM中的div,然后执行。
C)我发现它对于返回数据的格式和Google响应中包含的函数调用的参数名称有点困惑。
有其他人遇到同样的问题,并可以提供指导重新A通过C上面。
我附上下面的JavaScript代码。
$(document).ready(function() {
$('#viewbook-button').live('click', function() {
isbnCode = this.text;
alert("ISBN is : " + isbnCode + " " + this.text + " as well");
alert("Getting JSONP Google Books data");
isbnCode = "0451526538";
JSONPstr = '<' + 'script ' + 'src="' + 'https://www.googleapis.com/books/v1/volumes?q=ISBN' + isbnCode;
JSONPstr = JSONPstr + '&callback=handleJSONPResponse">' + '</script>';
alert("un-Escaped JSONP string" + JSONPstr);
escJSONPstr = escape(escJSONPstr);
alert("Escaped JSONP string");
//divstr = "";
//divstr = divstr + escape(<script src=");
//divstr = divstr + encodeURIComponent(https://www.googleapis.com/books/v1/volumes?q=ISBN);
//divstr = divstr + escape(isbnCode);
//divstr = divstr + encodeURIComponent(&callback=handleJSONPResponse);
//divstr = divstr + escape("></);
//divstr = divstr + escape(script);
//divstr = divstr + escape(>);
$('#jsonp-call').html(escJSONPstr);
// This will cause the handleJSONPResponse function to execute when the script is dynamically loadedinto div.
// The data wrapped in a function call will be returned from the Google Books server.
// This will cause the handleJSONPResponse function to execute below.
}); // end viewbook-button
}); // end document.ready
function handleJSONPResponse(response) {
var tmp = response;
alert(tmp);
};
</script>
</head>
<body>
<h2>Show Details of Books Ordered by a Customer</h2>
<a href="#" id="getcusts-button">Get Customer Details</a>
<br/><br/>
<div id="tablist">Tables will be Listed Here</div>
<br/><br/>
<div id="Google-call">The JSONP generated src= statement will go here and execute</div>
</body>
</html>
编辑:
问题解决了 - 感谢大家。
Joe,下次使用更通用的标签(如jQuery和Javascript),因为人们遵循特定的标签来找到他们可以提供帮助的问题。 – iwasrobbed 2011-12-20 15:03:38
如何关闭请求iWasRobbed – JimmyBandit 2011-12-20 18:16:57
不确定你的意思,乔。关闭什么请求? – iwasrobbed 2011-12-20 18:32:14