使用img.src跨域请求
生活随笔
收集整理的這篇文章主要介紹了
使用img.src跨域请求
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用Img.src跨域請求
無刷新的頁面請 求,被越來越多的應用。XMLHttp.Request只支持同域名的請求,Iframe支持跨域請求,但是跨域Javascript調用會被制止,使得 Iframe跨域請求沒有辦法做到CallBack。Script.Src的請求可以跨域,也可以寫一個比較復雜的Script機制讓它 CallBack(使用Script Request解決跨域請求問題), 這幾種實現方式都是比較重的。在一些場景下需要一個小巧的跨域請求。比如統計某個A Href鏈接被點擊了幾次,某個圖片被點擊了幾次。在這種場景下發送到服務器的數據量小,客戶Script不關心服務器返回結果的內容,只關心這次請求是 否成功。在這種場景下完全可以使用Img.src做異步跨域請求。
應用場景:
假設有個點擊統計服務器: http://CountHits.Com 它后面有數據庫統計每天某個鏈接被點擊了幾次。它對外提供的REST訪問接口是:
vender=venderID&href=urlHref&text=urlText&target=urlTarget
&style=urlStyle&location=pageLocation&refere=pageRefere.
vender 表示當前統計的投放者. location鏈接所在的頁面. refere當前頁面的來源頁面。如果統計成功返回200 狀態碼,如果統計失敗返回500狀態碼。
根據這樣的REST-URL接口,使用Img.src發送點擊統計請求。
[1] 只為某個鏈接加統計,失敗時重試3次:
[2] 給所有鏈接加統計,失敗時重試3次:
服務器端輸出代碼
轉自:http://zhangsichu.com/blogview.asp?Content_Id=102
無刷新的頁面請 求,被越來越多的應用。XMLHttp.Request只支持同域名的請求,Iframe支持跨域請求,但是跨域Javascript調用會被制止,使得 Iframe跨域請求沒有辦法做到CallBack。Script.Src的請求可以跨域,也可以寫一個比較復雜的Script機制讓它 CallBack(使用Script Request解決跨域請求問題), 這幾種實現方式都是比較重的。在一些場景下需要一個小巧的跨域請求。比如統計某個A Href鏈接被點擊了幾次,某個圖片被點擊了幾次。在這種場景下發送到服務器的數據量小,客戶Script不關心服務器返回結果的內容,只關心這次請求是 否成功。在這種場景下完全可以使用Img.src做異步跨域請求。
應用場景:
假設有個點擊統計服務器: http://CountHits.Com 它后面有數據庫統計每天某個鏈接被點擊了幾次。它對外提供的REST訪問接口是:
vender=venderID&href=urlHref&text=urlText&target=urlTarget
&style=urlStyle&location=pageLocation&refere=pageRefere.
vender 表示當前統計的投放者. location鏈接所在的頁面. refere當前頁面的來源頁面。如果統計成功返回200 狀態碼,如果統計失敗返回500狀態碼。
根據這樣的REST-URL接口,使用Img.src發送點擊統計請求。
[1] 只為某個鏈接加統計,失敗時重試3次:
| function countClick(item){ // create Img item = item!=null && item.tagName ? item : this; var image = document.createElement("IMG"); image.src = "http://CountHits.Com/Handler.ashx? vender=0&href="+encodeURIComponent(item.href)+"&text="+encodeURIComponent(item.innerHTML)+ "&target="+encodeURIComponent(item.target)+"&style="+encodeURIComponent(item.style.cssText)+ "&location="+encodeURIComponent(window.location.href) +"&referrer=" + encodeURIComponent(document.referrer) + "&t="+ new Date().getTime(); image.height = 0; image.width = 0; item.requestTimes = item.requestTimes ? item.requestTimes ++ : 0; image.onerror = Function.createDelegate(item, retry); image.onload = Function.createDelegate(item, clean); //send request. document.body.appendChild(image); return true; } function retry() { if(this.requestTimes < 3) { this.requestTimes ++; countClick(this); } else { this.requestTimes = 0; } } function clean() { this.requestTimes = 0; } Function.prototype.createDelegate = function(instance, method) { return function() { return method.apply(instance, arguments); } } |
| <a href="http://www.zhangsichu.com" οnclick="countClick(this)" target="_blank">Test</a> |
[2] 給所有鏈接加統計,失敗時重試3次:
| function AddEventHandle(target,eventType,handler) { if(target.AddEventListener) { target.AddEventListener(eventType,handler,false); } else if(target.AttachEvent) { target.AttachEvent("on"+eventType,handler); } else { target["on"+eventType]=handler; } } function RemoveEventHandle(target,eventType,handler) { if(target.RemoveEventListener) { target.RemoveEventListener(eventType,handler); } else if(target.DetachEvent) { target.DetachEvent("on"+eventType,handler) } else { target["on"+evnetType]=null; } } function window_onload() { var links = document.getElementsByTagName("A"); //debugger; for(var i=0; i<links.length; i++) { AddEventHandle(links[i] , "click", Function.createDelegate(links[i], countClick)); } } |
服務器端輸出代碼
| public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { //Your code to sum hits // context.Response.ContentType = "image/gif"; System.Drawing.Bitmap image = new System.Drawing.Bitmap(1,1); image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif); } public bool IsReusable { get { return false; } } } |
轉載于:https://www.cnblogs.com/johnwonder/archive/2010/11/03/1867753.html
總結
以上是生活随笔為你收集整理的使用img.src跨域请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux网络编程常用函数详解与实例(s
- 下一篇: 斯诺基金会是什么