您的位置:首页 > Web前端 > JavaScript

跨域的jsonp

2016-07-01 08:35 295 查看
ajax的跨域问题

先是后台的代码  模拟传输的数据

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class JsonpServlet extends HttpServlet {

@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String m = request.getParameter("m");
if(m.equals("getJson")){
String json = "aaa({\"hello\":\"helloWorld\"})";
response.getWriter().print(json.toString());
}
}

}


然后我们在前台进行ajax的请求

<script type="text/javascript">
function getMess(){
$.ajax({
url:"http://localhost:8080/jsonp/jsonpServlet?method=getJson",
type:"get",
async:"false",
data:"",
jsonpCallback:"aaa",
success:function aaa(data){
alert(data.hello);
},
dataType:"jsonp"
});
}
</script>
<body>
<input type="button" value="获取值" onclick="getMess()">
</body>


jsonpCallback
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: