自己起了一个server,然后进行跨域访问,发现 下面的function函数没有执行,json的数据undefined ,代码如下
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function test(){
$.getJSON("http://localhost:6666/AutoServer/autocomplete?callback=?",{query:"b",max:"2"},
function (json){
alert(" ");
});
}
</script>
</head>
<body>
<input type="button" value="跨域" id="test" onclick="test()"/>
<div id="images">
</div>
</body>
希望牛人指点
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function test(){
$.getJSON("http://localhost:6666/AutoServer/autocomplete?callback=?",{query:"b",max:"2"},
function (json){
alert(" ");
});
}
</script>
</head>
<body>
<input type="button" value="跨域" id="test" onclick="test()"/>
<div id="images">
</div>
</body>
希望牛人指点
解决方案 »
- jquery如何快速显示list数据
- 表格打印问题
- frameset和action的问题
- 用JavaBean把注册信息写入数据库出现的棘手问题!!搞了一下午不知错在何方!!各位大虾快来指点迷津~~~~~
- IKAnalyzer分词器的问题
- eceived WS-I BP non-conformant Unquoted SoapAction HTTP header错误
- JSP中如何得到当前页面的带参数地址啊?
- 100分求,如何在网页里显示大对象数据
- 怎样才能使用RESLUTSET 获得SQL SEVER中的列的名字?大虾看过来。
- 五十分问一个简单问题:怎么用jsp做级联删除
- 菜鸟求指教 struts2.0 property标签里值的问题
- Myeclipse连接SQLserver问题
String callback= request.getParameter("callback");
response.getWriter().print(callback+"("+json.toString()+")");//回传json
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/jquery-1.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn_q").click(function(){
$.getJSON("test.action", function(data){
$("#content2").html(data);
});
});
});
</script>
</head>
<body>
<input id="btn_q" type="button" value="click"/>
<div id="content2"></div>
</body>
</html>
如果需要传参数<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/jquery-1.3.js"></script>
<script type="text/javascript">
function onaction(val){
$.getJSON("test.action&id="+val, function(data){
$("#content2").html(data);
});
}
</script>
</head>
<body>
<input id="btn_q" type="button" value="click" onclick="onaction(2)"/>
<div id="content2"></div>
</body>
</html>