如何在javascript中获取跨域请求的响应

发布于 2024-11-06 21:08:03 字数 1055 浏览 0 评论 0原文

我通过动态添加脚本并将其 src 属性设置为我需要向其发出请求的域来在 javascript 中发出跨域请求。 供参考:http://alvinabad.wordpress.com/2009/02/13/feb13 /

脚本代码:

var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', crossDomainURL);
script.setAttribute('id', 'script_id');

script_id = document.getElementById('script_id');
if (script_id) {
    document.getElementsByTagName('head')[0].removeChild(script_id);
}

现在,我需要解析这个请求的Response。我已经检查了提琴手的原始响应。数据在那里,但不在dom中。它是这样开始的:

<script type="text/javascript">
/* <![CDATA[ */
    if (top == self || parent != top || document.location.hostname != document.domain) 
    {  
        top.location.replace("http:\/\/www.facebook.com\/?gringotts_redir");}
/* ]]> */
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"**... then the rest

页面源代码显示了我嵌入的 JavaScript,我如何解析从该代码生成的数据。

I am making a cross domain request in javascript by dynamically adding a script and setting its src attribute to the domain that I need to make request to.
For reference: http://alvinabad.wordpress.com/2009/02/13/feb13/

Script code:

var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', crossDomainURL);
script.setAttribute('id', 'script_id');

script_id = document.getElementById('script_id');
if (script_id) {
    document.getElementsByTagName('head')[0].removeChild(script_id);
}

Now, I need to parse the Response of this request. I have checked the Raw Response from fiddler. The data is there, but it's not in the dom. It starts like this:

<script type="text/javascript">
/* <![CDATA[ */
    if (top == self || parent != top || document.location.hostname != document.domain) 
    {  
        top.location.replace("http:\/\/www.facebook.com\/?gringotts_redir");}
/* ]]> */
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"**... then the rest

The page source shows the javascript embedded by me, how do I parse the data that's generated from that code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿哥们儿 2024-11-13 21:08:03

通常完成此操作的方法是让脚本响应包含对页面上已存在的函数的函数调用。但是,出于安全原因,浏览器不会让您的代码“查看”导入脚本的内容。不过,只要脚本是有效的 JavaScript 代码,浏览器就会运行该脚本。在您的情况下,响应是不是有效的 JavaScript 代码。不能有

The way this is generally done is to have the script response consist of a function call to a function already present on the page. The browser will not let your code "see" the contents of the imported script, however, for security reasons. The browser will run the script, however, so long as it's valid JavaScript code. In your case, the response is not valid JavaScript code. There can't be a <script> tag or any HTML markup — it must be pure JavaScript code, just like the contents of any other file imported with a <script> tag.

帅冕 2024-11-13 21:08:03

您要求的内容称为 JSONP 或“带填充的 JSON”。

请参阅:http://en.wikipedia.org/wiki/JSONP 了解更多信息。

What your asking for is known as JSONP or "JSON with padding".

see: http://en.wikipedia.org/wiki/JSONP for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文