Cross-domain Ajax call

 

How to overcome cross-domain ajax call:

1, Use Proxy

ContractedBlock.gif ExpandedBlockStart.gif Client-side Code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    
<title>Untitled Page</title>
   
<script type="text/javascript" src="../JS/Framework.js"></script> 
   
<script type="text/javascript" >
   
   
function test()
   {
        
var div = document.getElementById("status");
        
var request = MSXMLUtil.createXMLRequest();
        
var url = "Proxy.aspx?id=1&name=xiaolin";
        request.open(
"POST", url, true);
        request.onreadystatechange
=handleChange(request, div);
        request.setRequestHeader(
"Content-Type""application/x-www.form-urlencoded");
        request.send(
null);
        div.innerHTML
="Loadingdot.gif";
   }
   
   
function handleChange(request, div)
   {
        
return function()
        {
            
if(request.readyState==4)
            {              
                
if(request.status==200)
               {   
                   div.innerHTML
=request.responseText;
               }              
            }
        }
   }   
     
</script> 
</head>
<body>
    
<input type="button" value="Cross-domain Ajax Call" onclick="test()" />
    
<div id="status" style="background-color:Blue; color:White"></div>
   
</body>
</html>

 

ContractedBlock.gif ExpandedBlockStart.gif Server-Side Code
public class AjaxProxy
{
    
public static string TransferAjaxCall(string postData)
    {
        HttpWebRequest request 
= WebRequest.Create("http://abc.com/test/handle.aspx"as HttpWebRequest;
        
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(postData);
        
if (request != null)
        {
            request.Method 
= "POST";
            request.ContentType 
= "application/x-www-form-urlencoded";
            
//Detect the proxy.
            request.Proxy = WebProxy.GetDefaultProxy();
            Stream postStream 
= request.GetRequestStream();
            postStream.Write(buffer, 
0, buffer.Length);
            postStream.Close();

            HttpWebResponse webResponse 
= (HttpWebResponse)request.GetResponse();

            Encoding resEncoding 
= Encoding.GetEncoding("UTF-8");
            StreamReader reader 
= new StreamReader(webResponse.GetResponseStream(), resEncoding);

            
string result = reader.ReadToEnd();
            reader.Close();
            webResponse.Close();

            
return result;
        }
        
else
        {
            
return string.Empty;
        }
    }
}

public partial class CrossDomainAjax_Proxy : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        
string postData = "id=" + 1 + "&name=" + "xiaolin";
        
string ret = AjaxProxy.TransferAjaxCall(postData);
        ViewState[
"Ret"= ret;
    }

    
protected override void Render(HtmlTextWriter writer)
    {
        
if (ViewState["Ret"!= null)
        {
            writer.Write(ViewState[
"Ret"].ToString());
        }
    }
}

2, Use src property of <script></script> object to call cross-domain server.

3, Still thinking...

转载于:https://www.cnblogs.com/Roy_Zhou/archive/2009/04/21/1440667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值