一、前端jsjavascript
一、參數定義: var sendJSONP = function(url){ var settings = { url:url, type:"GET",//JSONP下只能GET dataType:"jsonp", async:false,//同步請求,在JSONP下無效 jsonp: "callback",//指定參數名 jsonpCallback: "jsonp",//指定回調函數名,參數名和回調函數名在url中會被拼接成..&callback=jsonp&.. headers: {'Content-Type': 'application/json;charset=utf-8'} } return promising_ajax(url,settings); } 二、promise定義: var promising_ajax = function(url, settings) { settings = settings || {}; return new Promise(function(resolve, reject) { settings.success = function(data, status, jqXHR) { resolve(data); }; settings.error = function(jqXHR, status, error) { reject(wrap_ajax_error(jqXHR, status, error)); }; ajax(url, settings); }); }; 三、請求發送: var ajax = function (url, settings) { // like $.ajax, but ensure XSRF or Authorization header is set if (typeof url === "object") { // called with single argument: $.ajax({url: '...'}) settings = url; url = settings.url; delete settings.url; } return $.ajax(url, settings); };
二、後端代碼:前端
@RequestMapping(value="/openapi/add/{trainId}",method= RequestMethod.GET,produces = "application/json;charset=utf-8") public void add(@PathVariable Integer trainId, String data, String callback, HttpServletResponse response) throws IOException { response.setHeader("Content-type","application/json;charset=UTF-8"); response.getWriter().write(callback+"("+ JSON.toJSONString(obj)+")"); }
三、使用方法java
service.sendJSONP(url).then(function(resdata){ if(resdata.result == 1){ } })
jquery ajax經常使用方法查詢:http://www.365mini.com/page/jquery_ajax.htmjquery