方式一
$.getJSON(OSL_SEARCH+"/search/doHotwords4Other.html?jsoncallback=?",function(json){ alert(json); //要求远程请求页面的数据格式为: ?(json_data) });
方式二
$.ajax( { type:'GET', url : OSL_SEARCH+"/search/doHotwords4Other.html", dataType:'jsonp', jsonp: 'jsoncallback', //默认callback error : function() { alert("Error"); }, success : function(data) { alert(data); } });
方式三
在Jquery AutoComplete中:
$("#searchHome").autocomplete(OSL_SEARCH+"/search/doSubCat4Other.html?jsoncallback=?",{ minChars : 0, selectFirst:false, scroll : false, dataType:'jsonp', jsonp: 'jsoncallback', extraParams:{param:function(){return shMainType;}}, parse: function(data) { return $.map(data, function(row) { return { data: row, value: row[1], result: row[1] }; }); }, formatItem: function(row, i, max) { return row[1]; }, formatMatch : function(e, d, c) { return e[1]; }, formatResult : function(row) { return row[1]; } } );
后台struts 代码demo
import java.io.IOException; import java.io.PrintWriter; import org.apache.struts2.ServletActionContext; import org.json.simple.JSONObject; import com.opensymphony.xwork2.ActionSupport; public class TestAction extends ActionSupport { /** * */ private static final long serialVersionUID = 1L; private JSONObject json; public void doAjax(){ System.out.println("come here doAjax"); json = new JSONObject(); json.put("D_A", "THomas trest"); String callback = ServletActionContext.getRequest().getParameter("jsoncallback"); try { PrintWriter out = ServletActionContext.getResponse().getWriter(); out.print(callback+"("+json+")"); out.flush(); } catch (IOException e) { e.printStackTrace(); } } public JSONObject getJson() { return json; } public void setJson(JSONObject json) { this.json = json; } }