我正在开发一个页面,通过jQuery的AJAX支持从Flickr和Panoramio中提取图像.
Flickr端工作正常,但当我尝试$.get(url, callback)从Panoramio时,我在Chrome的控制台中看到一个错误:
XMLHttpRequest无法加载http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&callback=processImages&minx=-30&miny=0&maxx=0&maxy=150.Access-Control-Allow-Origin不允许使用null.
如果我直接从浏览器查询该URL,它可以正常工作.发生了什么,我可以解决这个问题吗?我是不是错误地编写了我的查询,或者这是因为Panoramio会妨碍我正在尝试做的事情?
Google没有在错误消息上显示任何有用的匹配项.
编辑
以下是一些显示问题的示例代码:
$().ready(function () {
var url = 'http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&callback=processImages&minx=-30&miny=0&maxx=0&maxy=150';
$.get(url, function (jsonp) {
var processImages = function (data) {
alert('ok');
};
eval(jsonp);
});
});
Run Code Online (Sandbox Code Playgroud)
您可以在线运行该示例.
编辑2
感谢Darin对此的帮助. 上面的代码错了. 请改用:
$().ready(function () {
var url = 'http://www.panoramio.com/wapi/data/get_photos?v=1&key=dummykey&tag=test&offset=0&length=20&minx=-30&miny=0&maxx=0&maxy=150&callback=?';
$.get(url, function (data) {
// can use 'data' in here...
});
});
Run Code Online (Sandbox Code Playgroud) 我有以下网络服务;
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
它是库存标准,没有对类装饰器进行任何改动.
我有这个jQuery方法;
var webMethod = "http://localhost:54473/Service1.asmx/HelloWorld";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
url: webMethod,
success: function(msg){ alert(msg.d); },
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个后期操作,因为稍后我需要将数据发布到它.
当我执行jQuery时,我得到一个"无传输"错误.
我还应该提到的一点是,jQuery存储在我的机器上的一个简单的HTML文件中,而WebService也在我的机器上运行.
HTML页面上没有代码,它只是一个网页,而不是ac #project或任何东西.
任何人都可以指出我在正确的方向吗?
我试图用jQuery访问谷歌文档.这是我到目前为止所拥有的:
var token = "my-auth-token";
$.ajax({
url: "http://docs.google.com/feeds/documents/private/full?max-results=1&alt=json",
dataType: 'jsonp',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
},
success: function(data, textStatus, XMLHttpRequest) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
Run Code Online (Sandbox Code Playgroud)
如果我设置dataType为jsonp(来自使用jQuery创建跨域Ajax请求),它不允许我设置标头.如果我遗漏jsonp,我无法提出跨域请求.如果我使用jQuery.getJSON,我不能传入任何标题...
在制作跨域ajax请求时(在jQuery中)有没有办法定义自定义标头?
我正在使用jquery来构建对Twitter Search API的请求.我正在使用jsonp,这是跨域请求所需要的.但是,Twitter API指定您应为这些请求设置唯一的User-Agent,如果不这样做,则会限制您的请求.问题是,我认为无法通过jquery设置此标头.
这是我正在使用的代码:
$.ajax({
url: 'http://search.twitter.com/search.json',
dataType: 'jsonp',
type: 'get',
data: { q: 'twitter' },
success: function(data) {
alert(data.results);
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试过使用beforeSend方法,但看起来这个事件没有被触发.任何人都可以想出任何解决这个问题的方法吗?
谢谢.
jquery ×4
ajax ×2
jsonp ×2
cors ×1
cross-domain ×1
header ×1
http-headers ×1
javascript ×1
user-agent ×1
web-services ×1