jquery ajax jsp 傳回值亂碼二向解決方案

來源:互聯網
上載者:User

jquery ajax 網頁特效p/jsp.html target=_blank >jsp教程 傳回值亂碼二向解決方案在以前開發ajax時經常會碰到亂碼,今天我們用jquery ajax+jsp時,又出來亂碼了,經常測試找到了二種處理jquery ajax亂碼問題,下面看看。

<script>
$().ready(function(){
var url="/www.111cn.net/ajaxgetnewsclassfather.do";
var pars="random="+math.random();
var newsclassfather= $('#newsclassfather');
        $.ajax({
            url : url,
            data : pars,
            type :"post",
            success :function(res){
                newsclassfather.append(res);
            }
        });
    });

</script>

<%
//ajaxgetnewsclassfather.do

public void ajaxgetnewsclassfather(writer writer, model model, https教程ervletrequest request, httpservletresponse response)
    {
        response.setcontenttype("text/html");
        response.setcharacterencoding("utf-8");
        string dbname= cookieutil.getdbname(request, response);
        list<newsclass> newsclasslist= newsclassjdbcservice.findbybclassid(dbname,0);
        string result="";for (newsclass newsclass : newsclasslist)
        {
            result+="<option value='"+ newsclass.getclassid()+"'>"+ newsclass.getclassname()+"</option>";
        }try
        {
            writer.write(result);
        }catch (ioexception e)
        {
            e.printstacktrace();
        }
    }
/*
傳回值全部為亂碼

解決方案一

<%@ page language="java" contenttype="text/html; charset=utf-8"%>

解決ajax亂方法二,加個過濾器

*/

package com.zlm.tools;
import java.io.ioexception;
import javax.servlet.filterchain;
import javax.servlet.filterconfig;
import javax.servlet.servletexception;
import javax.servlet.servletrequest;
import javax.servlet.servletresponse;

public class tounicode implements javax.servlet.filter {
    private string tounicode = "iso-8859-1";//這個和你web 裡傳過來的一致;

    public void destroy() {

    }

    public void dofilter(servletrequest request, servletresponse response,
            filterchain chain) throws ioexception, servletexception {
        request.setcharacterencoding(this.tounicode);
        response.setcharacterencoding(this.tounicode);

        chain.dofilter(request, response);

    }

    public void init(filterconfig filterconfig) throws servletexception {
       
        string unicode = filterconfig.getinitparameter("tounicode");
       
        if (unicode != null && unicode.length() > 0) {
           
            this.tounicode = unicode;
       
        }

    }

}

%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.