Home > Article > CMS Tutorial > How to implement DedeCMS digg Ajax cross-domain
DedeCMS digg How to implement cross-domain Ajax?
Because the web project uses a second-level domain name, the original digg cannot be used normally. After careful analysis, it was found that Ajax JS submission cannot be submitted across domains.
Recommended study: 梦Weavercms
provides the following solutions:
@written by etongchina 2009-02-06 19:00
Implementation plan: similar to json implementation
Implementation principle: js allows imported remote files (js) to operate local data
Specific method: (with http://news.xxx. com/200812/25-4653.html as an example)
1. Modify the js calling part of http://news.xxx.com/200812/25-4653.html;
Write in the local html or js file:
<SCRIPT LANGUAGE="JavaScript"> function _Digg(type,tid){ var s = document.createElement("SCRIPT"); s.id="cgi_emotion_list"; document.getElementsByTagName("HEAD")[0].appendChild(s); s.src="http://www.xxx.com/../dig.php?type="+type+"&tid="+tid ; //需要统计的php页面的 src } function visitCountCallBack(data){ document.getElementsByTagName("HEAD")[0].removeChild(document.getElementById("cgi_emotion_list")); for(var i in data){ var e =document.getElementById(i); if(e) e.innerHTML=data[i]; //一些代码去修改本地html } } </script>
Modify the following code: 3480a33610d32e976ec8d549dc8746f7Like5db79b134e9f6b82c0b36e0489ee08ed
is: cfab4601ec966302d2ce5eae5eccc014Like5db79b134e9f6b82c0b36e0489ee08ed
2. Access remote files:
Remote file (http://www.xxx.com/../dig.php?type=digg&tid=456) returns similar code:
visitCountCallBack({ "visitcount":135 });
The above code is equivalent to the remote file calling the local function: visitCountCallBack
In this way, you can use remote return data to dynamically modify local files.
3. Summary:
Regarding this solution, it is currently feasible, but some people think it will be outdated. I don't think there will be any problem of JS overstepping its authority.
I have an AJAX-like application here. The key technology is the application of the src attribute of the 3f1c4e4b6b16bbbd69b2ee476dc4f83a tag.
Please take a look at the following HTML code
<HTML> <HEAD> <title>异步json例子</title> <SCRIPT LANGUAGE="JavaScript"> function test(){ var s = document.createElement("SCRIPT"); s.id="cgi_emotion_list"; document.getElementsByTagName("HEAD")[0].appendChild(s); s.src="http://g2.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=123456"; // test=function(){}; } function visitCountCallBack(data){ document.getElementsByTagName("HEAD")[0].removeChild(document.getElementById("cgi_emotion_list")); for(var i in data){ var e =document.getElementById(i); if(e) e.innerHTML=data[i]; } } </SCRIPT> </HEAD> <BODY> <button onclick="test()">test</button><BR> 历史访问人数:<span id="visitcount" style="color:#6600CC">点击test按钮获取数据</span><BR> 今天访问人数:<span id="dayvisit" style="color:#CC6633">点击test按钮获取数据</span><BR> 阳光指数:<span id="sun" style="color:red">点击test按钮获取数据</span><BR> 爱心指数:<span id="love" style="color:violet">点击test按钮获取数据</span><BR> 雨露指数:<span id="rain" style="color:blue">点击test按钮获取数据</span><BR> 营养指数:<span id="nutri" style="color:green">点击test按钮获取数据</span><BR> 花匠级别:<span id="gardener" style="color:#996633">点击test按钮获取数据</span> </BODY> </HTML>
You can copy the above code to your local computer and open it with IE or FIREFOX. Click the button. I found that the dynamic effect was achieved without refreshing the page, and the returned data was obtained across domains. Everyone knows that JAVASCRIPT cannot be accessed across domains, which is amazing. . . . Careful study of the code revealed the wonders
This code:
var s = document.createElement("SCRIPT"); s.id="cgi_emotion_list"; document.getElementsByTagName("HEAD")[0].appendChild(s); s.src="http://g2.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=123456";
The browser obtained the SCRIPT element through DOM parsing, and then added the ID and SRC attributes. Here is the official explanation of the SRC attribute of the SCRIPT element from the W3C specification: The script element allows authors to include dynamic script in their documents. When the src attribute is set, the script element refers to an external file. The value of the attribute must be a URI (or IRI). If the src attribute is not set, then the script is given by the contents of the element. Interpreted as: If the SRC attribute of the SCRIPT tag is defined, the SCRIPT tag refers to an external file, and the attribute value Must be a URL. This means that SCRIPT will reference the contents of the file from this URL. Everyone accesses this link in the browser: http://g2.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=123456 This URL similar to JAVA's SERVLET returns the following data: visitCountCallBack({"visitcount": 65188579, "dayvisit":8658, "spacemark":0, "markchange":0, "sun":1680, "love":478, "rain":1680, "nutri":1450, "level":5, "gardener":1});This string is a JAVASCRIPT function, and the input is a JSON string. When this data is returned, another JAVASCRIPT function above is called: function visitCountCallBack(data){
document.getElementsByTagName("HEAD")[0].removeChild(document.getElementById("cgi_emotion_list")); for(var i in data){ var e =document.getElementById(i); if(e) e.innerHTML=data[i]; } }
In the function, the JSON data returned by the innerHTML bar is used to fill the BODY, achieving asynchronous access to the data without refreshing the page. Effect. There is another key question: http://g2.qzone.qq.com/fcg-bin/cgi_emotion_list.fcg?uin=123456 is a URL of QQ space (uin is QQ number, you can enter your own QQ number to try (Test), JAVASCRIPT calls data from other domains. This method is relatively simple to obtain data, and can also access data across domains. It is more suitable for some simple, small non-refresh effects. I am a little worried that if the browser is updated one day and denies this access method, the data obtained in this way may become unusable. I suggest that everyone use this method with caution! ! !
The above is the detailed content of How to implement DedeCMS digg Ajax cross-domain. For more information, please follow other related articles on the PHP Chinese website!