WEB_EasyUI中comboBox远程绑定json数据引起的访问跨域问题

来源:互联网 发布:淘宝天猫店多少钱 编辑:程序博客网 时间:2024/06/07 12:55

  使用EasyUI的comboBox插件调用服务器json数据的时候,在控件中显示不出来,最后找的解决方法是在返回json的response中加入代码:response.setHeader("Access-Control-Allow-Origin", "*");下面列出代码:

  客户端(jsp页面):

<input class="easyui-combobox"
name="selectcombox"
data-options="
url:'返回json的地址',
method:'get',
valueField:'id',
textField:'text',
multiple:true,
panelHeight:'auto'
">

在这里要返回的json数据的样式是一个数组:

[{ "id":1, "text":"text1" },{ "id":2, "text":"text2" },{ "id":3,
* "text":"text3", "selected":true },{ "id":4, "text":"text4" },{
* "id":5, "text":"text5" }]


 服务器端的处理:

@RequestMapping("/getcheckboxdata.do")
public void getcheckboxdata(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("nihao");
JSONObject respJson = new JSONObject();

JSONArray arr = new JSONArray();

JSONObject object = new JSONObject();
object.accumulate("id",1);
object.accumulate("text", "text1");

arr.add(object);

JSONObject object2 = new JSONObject();
object2.accumulate("id",2);
object2.accumulate("text", "text2");


arr.add(object2);
response.setHeader("Access-Control-Allow-Origin", "*");// 这是关键的地方
response.getWriter().write(arr.toString());

  }

一开始的问题是:combobox中的下拉列表总是显示不出内容来,在服务器中加入 response.setHeader("Access-Control-Allow-Origin", "*");// 这是关键的地方

就ok了。

0 0