ITeye博客

ajax兼容IE,FF问题

wxq594808632 · 2009-03-31 14:10 · ajax · Apache · Ajax · Struts · JavaScript · IE · Markdown

如题,废话不多说。

JSP页面

hello.jsp

 

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>AJAX提交页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript" src="ajax.js"></script>
   <script type="text/javascript">
   function doso(){
     var url = "actAction.do";
     var s="武晓强斯蒂芬34-*(&_(*!$^_*!&#!^$+!*#&&";
     s=encodeURIComponent(s);      //转换特殊字符
     var parameter ="name="+s+"&email=abc@abc.com&www=http://wxq594808632.blog.163.com/";
     var method = "POST";
     function callBack(text){           //回调函数
      alert("调用成功!\n名字为:"+text);
     }
   new Ajax().ajaxRequest(url,parameter,method,callBack);     //调用方法发送Request
   }
   </script>
</head>
<body>
<input type="button" value="提交" onclick="doso()"/>
   </body>
</html>


 

 

 

java类

ActAction.java

 

package ajaxpost;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.io.PrintWriter;

public class ActAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws
            UnsupportedEncodingException {
        //设置字符编码返回的编码
        response.setContentType("text/html;charset=UTF-8");
        //接收字符的编码
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String www = request.getParameter("www");
        System.out.println(email);
        System.out.println(name);
        System.out.println(www);
     //   out.print(name+"\n"+email+"\n"+www);
        out.print(name);         //返回值
        return null;
    }
}


 

 

 

 

最重要滴

ajax.js

我用别人的。感谢这位大哥。。

/*
author zhangshuling
email  zhangshuling1214@126.com
*/
function  Ajax(){
    var _xmlHttp = null;
 this.createXMLHttpRequest = function(){
  try{
   if (window.ActiveXObject) {                                                     
    _xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                                      
   }                                                                               
   else if (window.XMLHttpRequest) {                                                   
    _xmlHttp = new XMLHttpRequest();                                                
   }
  }catch(e){
     alert(e.name +" : " + e.message);
  }
 }
 
 this.backFunction = function(_backFunction){
  if(_xmlHttp.readyState == 4) {
   if(_xmlHttp.status == 200) {
    _backFunction(_xmlHttp.responseText);//这里可以设置返回类型
   }
  }
   _xmlHttp.onreadystatechange = null;
 }

 this.doPost = function(_url,_parameter,_backFunction){
     try{
      _xmlHttp.open("POST",_url, false); 
   _xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   _xmlHttp.send(_parameter); 
   }catch(e){
    alert(e.name +" : " + e.message);
     }
 }
 
 this.doGet = function(_url,_parameter,_backFunction){
    try{
        var _random = Math.round(Math.random()*10000);
        _xmlHttp.open("GET", (_url+"?random=" +_random +"&" + _parameter), false); 
     _xmlHttp.send(null); 
   }catch(e){
      alert(e.name +" : " + e.message);
   }
 }
 
    this.ajaxRequest = function(_url,_parameter,_method,_backFunction){
          try{
            this.createXMLHttpRequest();
         if(_method.toLowerCase() == "post"){
            this.doPost(_url,_parameter,_backFunction);
         }else{
            this.doGet(_url,_parameter,_backFunction);  
         }
         try{
           _xmlHttp.onreadystatechange = this.backFunction(_backFunction);
         }catch(err){
            //??????IE?????????????????
         }
      }catch(e){
      alert(e.name +" : " + e.message);
   }
     }

} 

/*
 var url = "ajax.do";
 var parameter = "parameter=parameter";
 var method = "post" 
 
 function callBack(text){
  ....
 }
 
 new Ajax().ajaxRequest(url,parameter,method,callBack);

*/


 

经过本人FF,IE6,IE7,测试。完全好用

评论(4)

  1. #4 楼 · wxq594808632 · 2011-05-11 16:31

    haohao-xuexi02 writes:

    我问下,你现在还在维护这个程序吗?ie8是有问题?

    两年多了。现在都用jquery了。我之后就没有做web方向了

  2. #3 楼 · haohao-xuexi02 · 2011-05-10 12:00

    我问下,你现在还在维护这个程序吗?ie8是有问题?

  3. #2 楼 · wxq594808632 · 2009-05-10 21:55

    wh8766 writes:

    话说..博主能再排版下么 代码这直接贴的 啥都看不到了

    :cry:

    我导入rss的时候就是这样。。吓人。。管理员也没解决

  4. #1 楼 · wh8766 · 2009-05-03 11:30

    话说..博主能再排版下么 代码这直接贴的 啥都看不到了

评论功能维护中,当前仅展示历史评论。