使用jsonp实现ajax跨域访问
工作中的一个巧合,提到了ajax跨域访问的问题,刚刚心血来潮,弄了个简单的例子,为便于记忆,将前端和后端代码简单贴下:
前端:
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <html> <head> <title>ajax jsonp test</title> <script src="jquery/jquery-1.3.2.min.js" type="text/javascript"> </script> <script type="text/javascript"> $(document).ready(function(){ $("#button").click(function(){ $.ajax({ data:"?callback=?", url: 'http://192.168.1.170/resp.jsp', dataType:'jsonp', success:function(json){ $("#show").html(json); } }); }); }); </script> </head> <body> <form id="form1" name="form1" method="post"> <INPUT TYPE="button" value="提交" id="button"/> </form> <div id="show"> </div> </body> </html>
后端:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String callback = request.getParameter("callback"); String resp = "\"helloworld!\""; out.print(callback+"("+resp+")"); %>
相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05