实施 Google URL 缩短 API 的跨域问题
我正在尝试在 jQuery 的帮助下通过 AJAX 调用来实现 Google URL 缩短器 API。我做了这样的事情:
$(function() {
$('#btnshorten').click(function() {
var longURL = $('#tboxLongURL').val();
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
var result = eval(response); // Evaluate the J-Son response object.
}
});
});
});
但它在 IE 中生成错误。它显示“访问被拒绝”,并且在 Firebug 中显示“不允许 405 方法”。我在这里做错了什么吗?
I am trying to implement the Google URL shortener API with the help of jQuery by making an AJAX call. I have done something like this:
$(function() {
$('#btnshorten').click(function() {
var longURL = $('#tboxLongURL').val();
$.ajax({
url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: '{ longUrl: "' + longURL +'"}',
dataType: 'json',
success: function(response) {
var result = eval(response); // Evaluate the J-Son response object.
}
});
});
});
But it is generating an error in IE. It is showing "Access is denied" and in Firebug it is showing "405 method not allowed." Am I doing something wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Javascript 中,有 2 种方法可以实现 Google URL 缩短 API:
方法 #1:使用 jsonlib,
http://call.jsonlib.com/jsonlib.js
在这里尝试一下:http://jsfiddle.net/Qh4eR/方法 #2:使用 google 客户端库,
https:// /apis.google.com/js/client.js
,在这里尝试一下:http:// jsfiddle.net/pPHKe/2/In Javascript, here are 2 ways to to implement the Google URL shortener API:
METHOD #1: Using jsonlib,
http://call.jsonlib.com/jsonlib.js
Try it out here: http://jsfiddle.net/Qh4eR/METHOD #2: Using the google client library,
https://apis.google.com/js/client.js
, Try it out here: http://jsfiddle.net/pPHKe/2/恐怕你确实是这样。由于浏览器安全原因,您无法进行跨域 ajax 调用。
我知道 Ext JS 提供了一个 ScriptTagProxy 对象可以完成这项工作,但我不确定 jQuery 是否有类似的东西。
另一种方法是在您自己的主机上创建一种“代理”服务器端脚本,它可以接受来自 ajax 调用的参数,发出 HttpWebRequest 或类似于 googleapis.com 并输出响应,以便您再次获取阿贾克斯调用。然后只需修改您的 ajax url 参数即可调用新的代理脚本而不是 googleapis。换句话说——让服务器端来做跨域请求。
Indeed you are, I'm afraid. You can't make cross-domain ajax calls because of browser security.
I know that Ext JS offer a ScriptTagProxy object which can do the work, but I'm not sure if jQuery has anything similar.
An alternative would be to create a kind of "proxy" server-side script on your own host, which could accept parameters from your ajax call, make an HttpWebRequest or similar to googleapis.com and output the response to be picked up again by your ajax call. Then just modify your ajax url parameter to call your new proxy script instead of googleapis. In other words - let the server-side do the cross domain request.
您可以使用动态脚本标签进行跨域 ajax 调用。正如这里所指出的,这个方法有一些问题:很难知道内容何时可用,没有标准方法,并且可以被视为“安全风险”。
然而,该方法在我的情况下效果很好。请参阅此处了解很好的例子。该方法有点棘手。
You can use a dynamic script tag to make cross domain ajax calls. As pointed here this method has some problems: It's difficult to know when the content is available, there is no standard methodology, and can be considered a "security risk".
However the method works fine in my case. refer to here for a good example. The approach is a bit tricky.