如何使用JQuery实现FLXHR以进行跨域交互

时间:2010-12-26 08:59:09

标签: jquery

我正在使用JQuery,FLXHR从Cross Browser获取数据。

以下是示例 jquery代码:

        // set up the domain that we're going to call and tell flXHR not to try to parse the response as XML..
 $.flXHRproxy.registerOptions("http://staging/", {xmlResponseText:false});

 // set flXHR as the default XHR object used in jQuery AJAX requests
 $.ajaxSetup({transport:'flXHRproxy'});


 $.ajax({  
        type:"POST",        
        url: "http://staging/Login.aspx",  // Send the login info to this page
        data: str, 
        dataType: "json", 
        success: function(result)
        { 
             // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  
        } 

    });  

在上面的代码中,我使用JQuery和Flash进行跨浏览器交互,上面的代码工作正常,使用 http://staging/Login.aspx ,但是当我要去 https://staging/Login.aspx (HTTPS)身份验证它给我的错误(NS_ERROR_PROXY_CONNECTION_REFUSED)

请建议如何摆脱这个问题。

感谢。

1 个答案:

答案 0 :(得分:5)

我通过以下代码更改解决了我的问题。

我接受了以下文章的帮助。

1)http://www.offshootinc.com/blog/2010/03/12/cross-domain-scripting-with-jquery-and-flxhr/

2)http://tek-insight.blogspot.com/2010/05/cross-domain-ajax-request-proxy-json.html

3)http://flxhr.flensed.com

我们必须从 FLXHR网站下载插件,并且必须在所有 Javascript 的同一位置复制“部署”文件夹的内容保留。

以下是 crossdomain.xml 的示例代码,我们需要将其复制到您网站的根目录中。

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
  <site-control permitted-cross-domain-policies="master-only"/>
   <allow-access-from domain="*" secure="false"/>
   <allow-http-request-headers-from domain="*" headers="*"  secure="false"/>
</cross-domain-policy>

这里有 JQuery代码:

    var ajaxGatewayUrl = "https://staging/login/login.aspx";
    var loadPolicyURL = "https://staging/crossdomain.xml"

    // set flXHR as the default XHR object used in jQuery AJAX requests
    $.ajaxSetup({transport:'flXHRproxy'});
    // set up the domain that we're going to call and tell flXHR not to try to parse the response as XML..
    $.flXHRproxy.registerOptions(ajaxGatewayUrl,{xmlResponseText:false,loadPolicyURL:loadPolicyURL});

 //Submitting the form
$("#loginDetails > form").submit(function()
{
     //Hiding the Login button
    $("#loginButton").hide();

    //Showing the ajax loading image
    $("#ajaxloading").show();

    // 'this' refers to the current submitted form  
    var str = $(this).serialize();   
    // -- Start AJAX Call --

    $.ajax({ 
        type:"POST",        
        url: ajaxGatewayUrl,  // Send the login info to this page
        data:str, 
        cache:false,
        dataType: "json",        
        success: function(result)
        {  
             // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  

            if(result.Response!='NULL')
            {     

                     $('.validateTips').hide();
                    var login_response = '<div id="logged_in">' +
                     '<div style="width: 350px; float: left; margin-left: 80px;">' + 
                     '<div style="width: 40px; float: left;">' +
                     '<img style="margin: 22px 0px 10px 0px;" align="absmiddle" src="system/images/ajax-loader.gif">' +
                     '</div>' +
                     '<div style="margin: 24px 0px 0px 10px; float: right; width: 300px;">'+ 
                     "You are successfully logged in! <br /> Please wait while you're redirected...</div></div>";  

                    $('#loginButton').hide();
                    $('#closeBtn').hide();
                    $('#divMember').text(result.FirstName +' '+ result.LastName);
                    $('#spnSkywardsNo').text(result.ActiveCardNo);
                    $('#spnTierStatus').text(result.MemberShipTier);
                    $("#ui-dialog-title-skywardsLogin").text(getDataFromResourceFile('pleaseWait'));

                    $('#divSuccessLogin').html(login_response);
                    $('#divSuccessLogin').show();
                    $('#loginDetails').hide();



                    // After 3 seconds redirect the 
                    setTimeout(closeDialog, 3000); 
              }
            else// ERROR?
             {  
                 var login_response = getDataFromResourceFile('InvalidUsername');
                 $('.validateTips').html(login_response);
             }
        }, 
        error:function(request, textStatus, errorThrown)
        {
            // Show 'Submit' Button
            $('#loginButton').show();

            // Hide Gif Spinning Rotator
            $('#ajaxloading').hide();  

            $('.validateTips').html("Some Issue with requested URL");
        } 

    });  

    // -- End AJAX Call --

    return false; 
});

请查看上述实施情况,并提出良好意见。