日期:2014-05-17  浏览次数:20745 次

html页面前台如何获取后台JSON数据
import.jsp页面写的后台:
List t = allselect();
JSONArray json = new JSONArray().fromObject(t);
PrintWriter outpw = response.getWriter();
String jsonto = json.toString();
outpw.write(jsonto);
out.flush();
out.close();

html前台给如何去后台json的值
function import_data(){
$.ajax(  
{  
url: "import.jsp",  
dataType : "json",  
type: "post",  
timeout: 5000,  
success: showresult,  
error: function() { alert("error"); }  
}  
)
}
  function showresult(jsonto) {  
  alert(jsonto);  
  }  
<input type="button" value="数据导入" onClick="import_data()">
这样写有什么问题 为什么没数据出来
“import_data”的值为 null、未定义或不是 Function 对象

------解决方案--------------------
Java code

$(function() {
        $("#btn").click(function() {
            $.ajax({
                url : "import.jsp",
                dataType : "json",
                type : "post",
                timeout : 5000,
                success : showresult,
                error : function() {
                    alert("error");
                }
            });
        });
    });
    function showresult(jsonto) {
        alert(jsonto);
    }

<input type="button" value="数据导入" id="btn"/>