关于json后台传值到前台页面的问题
String data="{\'judge\':1}";
现在想要将上面的字符串传值到前台HTML文档的js中。事先有用Ajax进行传值到后台:
$.post("LoginAction.action",{"id":$("#id").val(),"psw":$("#psw").val()},function(data){
			alert("yes!");
			alert(data[0].judge);
		},"json");
问,怎么安全得到judge对应的值!
求知道的小伙伴给点建议!
么么哒!
------解决方案--------------------alert(data[0].judge);这个有值输出没有。
------解决方案--------------------
$.ajax({  
	type: "POST",  
	url:"LoginAction.action", 
	data:{"id":$("#id").val(),"psw":$("#psw").val()}, 
	dataType:'json',
	success: function(data){
           alert(data);
           }	   
)
你看看这样
------解决方案--------------------你的action是啥样的?可以写入到流内,页面是可以获取的。
------解决方案--------------------不写了,给你找一个例子参考吧。点这里
------解决方案--------------------dataType: "json",加上这个。
同时如果不行,用javascript将字符串转化为json
------解决方案--------------------你用severlet啊,很简单的。
------解决方案--------------------response.getWrite.write(data.toString())
------解决方案--------------------$.post("LoginAction.action",{"id":$("#id").val(),"psw":$("#psw").val()},function(data){
            alert("yes!");
      var val= eval(data);
            alert(val[judge]);
        },"json");
你的字符串也转不成数字不能用data[0]取值。
如果你的字符到前台了 ,这眼更就能输出了。
------解决方案--------------------你的后台写法对吗  ajax 还有return?
------解决方案--------------------你这个返回状体是成功了,走了后台 ,但是后台你也没有给前台值啊
------解决方案--------------------response.setCharacterEncoding("utf-8");
			PrintWriter pw = response.getWriter();
			pw.print(json);
			pw.flush();
后台的字符串这样输出,你用ajax 就不用转发了。直接return null ;就行了
------解决方案--------------------a code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    public String execute() throws 
IOException{
        System.out.println(id+"  "+psw);
        if(coi.LoginValidation(id, psw)){
            System.out.println("yes!");
             
        String data="{\'judge\':1}";            
        Gson gson=new Gson();
        gson.fromJson(data, Gson.class);
        System.out.println(data.toString());
response.setCharacterEncoding("utf-8");
PrintWriter pw = response.getWriter();
pw.print(data);
pw.flush();
   &nb