如何获得一个页面的json数据 jsp页面怎么发送json数据
如何获得一个页面的json数据
可以用下面代码试试:
public String IP() throws IOException{
String IP=null
URL url = new URL("http://iframe.ip138.com/ic.asp")
URLConnection conn = url.openConnection()
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows U Windows NT 5.1 en-US rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15")
conn.setRequestProperty("Content-Type", "text/html")
conn.setRequestProperty("Accept",
"text/html,application/xhtml xml,application/xmlq=0.9,*/*q=0.8")
InputStream is = conn.getInputStream()
BufferedReader br = new BufferedReader(new InputStreamReader(is,
"GB2312"))
String line = null
while ((line = br.readLine()) != null) {
System.out.println(line)
}
br.close()
return IP
}
jsp页面怎么发送json数据
jsp页面发送json数据是在ajax接口中指定dataType:
举例如下:
$.ajax({
                contentType: "application/json",
                url: "/WS/vinson.asmx/Logon",
                data: "{userName:"   name.val()   "}",
                type: "POST",
                dataType: "json", //这里指定传送的数据类型
                success: function (json) {
                    json = eval("("   json.d   ")")
                    if (json.success == "error") {
                        $("#divLogin").show()
                        alert(json.msg)
                        code.val("")
                        pass.val("")
                        pass.focus()
                        UpdateImage()
                    } else if (json.success == "success") {
                        $("#imgFace").attr("src", "/gamepic/face"   json.fid   ".gif")
                        var name = SubString(json.account, 12, "")
                        $("#dlInfo").html("<span>帐号:"   name   "</span><span>ID:"   json.gid   "</span>").attr("title", json.account)
                        $("#mOrder").html(json.morder)
                        $("#loves").html(json.loves)
                        $("#mPat a").before(json.UserPat " ")
                        $("#counts").html(json.counts)
                        if (parseInt(json.dayCount) > 0)
                            $("#qd ul").css("background", "url(../images/qiandao_n.png) 0 0px no-repeat")
                        else
                            $("#qd ul").css("background", "url(../images/qiandao_n.png) 0 -60px no-repeat")
                        $("#divLogin").hide()
                        $("#divInfo").show()
                    }
                },
                error: function (err, ex) {
                    alert(err.responseText)
                    UpdateImage()
                }
            })
        })
在jsp页面中获取json中的数据
可以用 js来解析JSON
用一个 变量 resultJson 来接收你传递过来的JSON
var resultJson = <%=operator.getUser()%>
resultJson.将是一个 JSON对象.
html页面前台怎么获取后台JSON数据
没有进success:function(res){}方法, 说明方法就不成功, 你可以到后台去看看是否报错, 如果没报错, 能进success :function(res) 之后,到页面 在里面用 console.log(res) 输出res 看数据结构,
success: function(res){
console.log(res)
}
到浏览器F12 看浏览器报错和输出的数据