crossDomain心跳无法使用jQuery解析jsonp数据 [英] crossDomain heartbeat cant parse jsonp data with jquery

查看:283
本文介绍了crossDomain心跳无法使用jQuery解析jsonp数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的模块(一个asp.net项目)嵌入门户"中,门户生成一个iframe到我的URL,我知道它很烂,但我没有做到. 为避免在用户迭代我的Web项目时在主要门户"端进行会话,门户网站所有者要求我通过javascript从应用程序到门户网站启动心跳.
每个人都知道以这种方式进行保持会话是不安全的,但是门户"在那里我无事可做.
真正的问题是我无法执行从我的应用程序到门户的跨域请求,因为相同的来源政策锁定它,我找到了一个使用jquery的解决方案,但是它需要[heartbeat listener]处理json.
官方jsonp网站此处.
有人可以帮助我吗?
有我的脚本:

I embbeding my module, an asp.net project, in a "portal", the portal generate an iframe to my url, i know its a shit but i dont made it.
To avoid session in main "portal" end while user iterating with my web project the portal owner told me to start an heartbeat by javascript from my application to portal.
Everyone know keep session in this way is insecure but 'portal' there is then i havent nothing to do.
The real problem is that i cant do cross-domain requests from my application to portal because same origin policy lock it, i found a solution using jquery but it require [heartbeat listener] deal with json.
The official jsonp site here.
Someone can help me?
there is my script:

function startHeartbeat() 
{
    var interval = 9513575;
    window.setInterval(
         function () {
             $.ajax({
                 type: "GET",
                 cache: false,
                 async: true,
                 crossDomain: true,
                 url: "http://www.theportalurl.com",
                 dataType: 'JSONP',
                 complete:function(jqXHR, textStatus){                    
                     alert("Complete");
                 },
                 success:function(json){                    
                     alert("Success");
                 },
                 error:function(jqXHR, textStatus, errorThrown){
                     alert("Error:" + textStatus + ", detail:" + errorThrown);
                 },
            });

         }
     , interval
     );
}

@rook给我帮助后,我达到了这个目标:

after @rook give me help i reach this:

function startHeartbeat(pgn) 
{
    $("body").append("<img id='heartbeat' style='width:1px; height:1px' name='heartbeat' src='http://www."+Math.random()+".org'/>");
    var interval = 350000;
    window.setInterval(
         function () {
            var rnd = Math.random();
            var url = "https://www.theportal.com/refreshsession.aspx?pgn="+pgn+"&rndv="+rnd;
            $("#heartbeat").attr("src", url);
         }
     , interval
     );
}

推荐答案

您要尝试的操作明显违反了JavaScript的相同来源策略.一个好的解决方案是,门户网站所有者可以为要使用XHR获取的页面(和页面)设置此http标头元素.

What you are trying to do is a clear violation of the same origin policy for JavaScript. A good solution is that the portal owner can set this http header element for the page (and only the page) that you want to fetch with an XHR.

Access-Control-Allow-Origin: http://foo.example

来源: http访问控制

这篇关于crossDomain心跳无法使用jQuery解析jsonp数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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