自己起了一个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>    
希望牛人指点

解决方案 »

  1.   

    后台服务:
    String callback= request.getParameter("callback");
    response.getWriter().print(callback+"("+json.toString()+")");//回传json
      

  2.   

    function (json) 改为function (data)
      

  3.   


    <%@ 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>
      

  4.   

    2楼正解,我就是在server端没有加jquery的id造成的