zoukankan      html  css  js  c++  java
  • JQuery Ajax jsonp

     JQuery ajax jsonp

      

    $.ajax({
            method:"POST",
            url:"http://localhost:8081/ChenLei/PeopleServlet",
            data:{"userName":"zhangsan"},
            dataType:"jsonp",
            jsonpCallback:"callback",   //jsonpCallback是实现跨域请求的时候定义回调函数用的 ,可以随意起名字
            success:function(data){
                alert(data.code);
                alert(data.message);
            },
            error:function(data){
            },
            complete:function(){
            }
        })
    

     后台如果是servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println(request.getParameter("userName"));
            String callback = request.getParameter("callback");
            response.getWriter().print(callback+"({"code":"200","message":"success!!"})");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
    }

     如果是Springmvc

    import com.fasterxml.jackson.databind.util.JSONPObject;


    @RequestMapping(value = "/chenlei/test/getuser", method = {RequestMethod.GET,RequestMethod.POST}) @ResponseBody public JSONPObject getUser(@RequestParam String userName, @RequestParam String callback){ Map<String, String> map = new HashMap<String, String>(); map.put("code", "200"); map.put("message", "success!!"); return new JSONPObject(callback, map);
    }
  • 相关阅读:
    Linux系统中常用操作命令
    CentOS 7 巨大变动之 systemd 取代 SysV的Init
    不可不知的安卓屏幕知识
    Gradle-jar-aar
    【Android】开源项目汇总
    Android中关于系统Dialog无法全屏的问题(dialog样式)
    Android LockScreen (锁屏弹窗)
    linux 权限设置
    android 多语言(在APP里面内切换语言)
    host更新
  • 原文地址:https://www.cnblogs.com/c9999/p/7481520.html
Copyright © 2011-2022 走看看