【基礎】Html跨域跳轉問題整理


今天遇到一個問題,是有關 跨域跳轉問題,涉及到知識比較基礎。

具體問題是:

 A站點的 PageA (Post數據)到 B站點的 PageB,PageB接受到后Redirect到B站的 PageC; 那么,當前瀏覽器中的PageA頁面 會被Redirect到PageC 嗎? 
 
測試了一下幾種情況:
1.普通ajax:
類似以下這種ajax,答案是否定的
$(function(){
    $('#send').click(function(){
         $.ajax({
             type: "GET",
             url: "http://localhost:PageB/", 
data: {username:$(
"#username").val(), content:$("#content").val()}, dataType: "json", success: function(data){ $('#resText').empty(); //清空resText里面的所有內容 var html = ''; $.each(data, function(commentIndex, comment){ html += '<div class="comment"><h6>' + comment['username'] + ':</h6><p class="para"' + comment['content'] + '</p></div>'; }); $('#resText').html(html); } }); }); });

 

2.通過Request模擬Post提交

類似以下這種后台模擬Request,答案是否定的

public  string HttpPost(string Url, string postDataStr, HttpForType hft) {

            SetAllowUnsafeHeaderParsing20(true);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
 
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.CookieContainer = cookie; //cookie信息由CookieContainer自行維護

            var sss11 = GetAllCookies(cookie);
             
            request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)";
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            HttpWebResponse response = null;
            try {
                SetCertificatePolicy();
                response = (HttpWebResponse)request.GetResponse();
            } catch (System.Exception ex) {
                return ex.Message.ToString();
            }
            //獲取重定向地址
            //string url1 = response.Headers["Location"];
            if (response != null) {
                var sss22 = GetAllCookies(cookie);

                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
string retString = response.GetResponseHeader("Location"); return retString; } else { return "error"; //post請求返回為空 } }

 

3.拼湊form表單形式Post提交  

類似以下這種ajax,答案是肯定的,可以直接從 A頁面 跳轉到C頁面(調試發現:B頁面跳轉C頁面是在B頁面后台處理的,處理完返回302跳轉碼和新地址到瀏覽器,302的新地址,header的location屬性值就是C頁面地址)

 

或者在A頁面插入類似以下表單形式,答案也是可以跳轉到C頁面:

 <form action="http://localhost:PageB/" method="post">
        <input id="name" name="name" value="123"/>

 

 

以上是討論的Post跨域跳轉問題。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM