首页 > 代码库 > jquery Jsonp的使用

jquery Jsonp的使用

  <script type="text/javascript">       $(function(){            $.ajax({                url:"test",                jsonpCallback: "showPrice",                jsonp: "callback",                dataType: "jsonp",                data: {                   q: "select title,abstract,url from search.news where query=\"cat\"",                   format: "json"                 },                success: function(data) {                      alert(data.name);                },                 error:function(){                     alert("s");                 }            });                    });            </script>

test servlet

/**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        String name = request.getParameter("callback");        request.getParameter("q");        System.out.println(name);        response.getWriter().write(name+"({name:\"bai\"})");    }

在写回的时候格式最好是callback({   })这种格式 ,否则接受时出粗到error,具体原理以后慢慢参详

jquery Jsonp的使用