了解跨域XHR和XML数据 [英] Understanding Cross domain XHR and XML data

查看:123
本文介绍了了解跨域XHR和XML数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用JavaScript和AJAX相当一段时间,我想了解如何跨域XHR真正的工作和jQuery如何处理它,由于某种原因,我从来没有费心去想想它到底是如何工作的。我已阅读维基百科JSONP 的文章,我更糊涂了。我不知道那是什么,我不理解。

I have been working with JavaScript and AJAX for quite sometime, I would like to understand how Cross Domain XHR really work and how JQuery handles it, for some reason I have never bothered to think about how it really works. I have read Wikipedia JSONP article and I am more confused. I am not sure what is that I am not understanding.

据我所知,使用 JSONP 我可以直接在JavaScript中消耗 JSON 数据。例如这个JS小提琴例子。在这里我使用 JSON 来显示图像的列表。我可以实现使用 XML 数据,而不是同样的事情?你回答这部分的问题之前,请先阅读类比的其余部分。

I am aware that using JSONP I can consume JSON data directly in JavaScript. For example this JS Fiddle example. Here I am using JSON to display list of images. Can I achieve the same thing using XML data instead? Please read rest of the analogy before you answer this part of the question.

1)如果我试图像下面或小提琴链接东西我得到错误未捕获的Ref​​erenceError:jsonFlickrFeed没有定义

1) If I try something like below or Fiddle link I get error Uncaught ReferenceError: jsonFlickrFeed is not defined

​$.ajax({
    url: "http://api.flickr.com/services/feeds/photos_public.gne",
    data: {
        format: "json"
    },
    dataType: "jsonp",
    success: function(d) {
        console.log(d);
    }
});​

2)示例下或小提琴链接正常工作

$.ajax({
    url : "http://api.flickr.com/services/feeds/photos_public.gne",
    data: {format: "json"},
    dataType: "jsonp"
});
jsonFlickrFeed = function(d){
    console.log(d);
}

Q)我猜想1和2之间,因为返回的数据格式,比如 jsonFlickrFeed({})我们需要写jsonFlickrFeed回调函数,使其工作?

Q) I suppose between 1 and 2 since returned data is in format like jsonFlickrFeed({}) we need to write jsonFlickrFeed callback function to make it work?

Q)为什么它永远不会调用成功的回调?

Q) Why does it never invoke success callback?

Q)是不是Flickr的终点,做返回JSONP的工作(我指的是在数据格式 jsonFlickrFeed({}))?或者,它只是返回的实际JSON和jQuery垫吗?

Q) Is it Flickr end point that does the job of returning JSONP(by which I mean data in format jsonFlickrFeed({}))? Or does it just return the actual JSON and JQuery pads it?

3)随着 $。的getJSON 的code是类似下面或的小提琴

3) With $.getJSON the code is something like below or fiddle

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {
    format: "json"
}, function(d) {
    console.log(d)
});​

Q)如何JQuery的照顾它的情况下3)?我看到,返回的数据格式 jQuery1820349100150866434_1355379638775({})所以,如果我认为jQuery不会的JSON的回调相关联的工作是正确的吗?

Q) How does JQuery take care of it in case 3)? I see that returned data is in format jQuery1820349100150866434_1355379638775({}) So if I assume that JQuery does the job of associating the JSON with the callback is it correct?

Q),由于上述原因,它被称为JQuery的速记方法

Q) For the above reason it is called shorthand method of JQuery?

这是什么我想我已经没有使用XML数据。我一直没能想出一个办法使用XML数据,而不是JSON。

From whatever I tried I have failed to consume XML data. I have not been able to think of a way to consume XML data instead of JSON.

Q)是否有可能以类似的方式,而不是使用JSON XML数据?

Q) Is it possible to use XML data instead of JSON in similar way?

Q)我能想到这样做,否则是进行代理通过同一个域中的数据的唯一途径。这是理解是否正确?

Q) The only way I can think of doing this otherwise is proxying the data through same domain. Is this understanding correct?

如果它帮助这里是 XML的例子,我对Dropbox的。这是证明当它从相同域来源于它XML数据可以被解析。

If it helps here is XML example I have on dropbox. Which is to demonstrate that it XML data can be parsed when it originates from the same domain.

推荐答案

@adeneo回答了这个问题,但在评论。所以我的理解有关 JSONP 是根本性的缺陷。当JSONP请求时,它不是一个XHR请求。相反,需要注意的是动态插入剧本标记并获取 JSON 。因此,即使,调用看起来像XHR(至少IMO的JQuery),事实并非如此。 XMLHtt prequest对象不使用的。

@adeneo answered the question but in comment. So my understanding about JSONP was fundamentally flawed. When JSONP request is made, it is not an XHR request. Rather, caveat is to insert script tag dynamically and fetch the JSON. So even though, the call looks like XHR(at least IMO JQuery), it is not. XMLHttpRequest object is not used at all.

这个问题已经有了答案什么是JSONP所有关于莫名其妙?但我之前错过了。另一个很好的资源解释跨域请求是 devlog

This question has already been answered What is JSONP all about? but I somehow missed it before. Another good resource explaining Cross Domain request is at devlog

其余的我都提出成为多余!

Rest of the issues I have raised become redundant!

这篇关于了解跨域XHR和XML数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆