Google Weather XML 上的跨域请求
我想使用 ajax 调用获取 google 天气 xml,但出现以下错误:
405 Method not allowed
进行跨域调用总是很麻烦,我知道我们可以使用 jsonp 来做到这一点,但 api 返回 xml。
var weather= function(){
url = 'http://www.google.com/ig/api?weather=karachi';
var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" +escape( url);
/*SERVER CALL FOR CROSS DOMAIN*/
//ServerCallRAW('http://www.google.com/crossdomain.xml', function(_dataCD){
//console.log(_dataCD);
ServerCallXML(url, function(_dataAPI){
console.log(_dataAPI);
//});
});
};
function ServerCallXML(_url,callback) {
$.ajax
({
type: "GET",
url: _url,
dataType: 'xml',
//contentType: "application/json; charset=utf-8",
contentType: "text/xml; charset=iso-8859-1",
async: false,
//data: {'json':'{"username":"' + username + '", "password":"' + password + '"}'},
success: function (result) {
callback(result);
}
});
};
谢谢
I want to grab the google weather xml, using ajax call but I 'm getting the following error:
405 Method not allowed
Its always been a trouble to make a cross domain call, I know we can do it using jsonp but the api is returning xml.
var weather= function(){
url = 'http://www.google.com/ig/api?weather=karachi';
var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=" +escape( url);
/*SERVER CALL FOR CROSS DOMAIN*/
//ServerCallRAW('http://www.google.com/crossdomain.xml', function(_dataCD){
//console.log(_dataCD);
ServerCallXML(url, function(_dataAPI){
console.log(_dataAPI);
//});
});
};
function ServerCallXML(_url,callback) {
$.ajax
({
type: "GET",
url: _url,
dataType: 'xml',
//contentType: "application/json; charset=utf-8",
contentType: "text/xml; charset=iso-8859-1",
async: false,
//data: {'json':'{"username":"' + username + '", "password":"' + password + '"}'},
success: function (result) {
callback(result);
}
});
};
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在您的服务器上创建本地代理以避免跨域请求。也就是说,您本质上创建一个执行 cURL(php) 或 HttpRequest(c# 等) 的页面,然后将 cURL/HttpRequest 的内容呈现为响应。与 javascript(客户端代码)不同,服务器通常不限于仅发出相同域的请求。
You can create a local proxy on your server to avoid the cross-domain request. That is you essentially create a page that performs a cURL(php) or HttpRequest(c# etc) then renders the contents of the cURL/HttpRequest as it's response. Unlike javascript (client side code) servers are not typically restricted to making only same-domain requests.