如何在javascript中获取跨域请求的响应
我通过动态添加脚本并将其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常完成此操作的方法是让脚本响应包含对页面上已存在的函数的函数调用。但是,出于安全原因,浏览器不会让您的代码“查看”导入脚本的内容。不过,只要脚本是有效的 JavaScript 代码,浏览器就会运行该脚本。在您的情况下,响应是不是有效的 JavaScript 代码。不能有
标记或任何 HTML 标记 - 它必须是纯 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.您要求的内容称为 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.