Plugin description: As we all know, the use of cross-domain ajax directly initiate problem-free access to the request, this time need to use jsonp protocol (unofficial protocol) processing, jQuery in
1, js page
code>
function login(){
var ip="http://192.168.31.6:8080/HTML_ht";
$.ajax({
type:"GET",
url:ip+"/login.jsp",
crossDomain: true,
data:{
username:'admin',
password:'123',
},
dataType:'JSONP',
jsonp:"callback",
async:true,
success:function(data){
var username = data.username;
var password = data.password;
if(username=="admin" && password=="123"){
alert("登陆成功");
}else{
alert("登陆失败");
2, h5 page
<code>
<div class="container">
<input type="text" name="username" id="username" />
<input type="password" name="password" id="password" />
<input type="button" value="Login" onclick="login()"/>
</div>
3, background processing
public void testjson(HttpServletRequest request, HttpServletResponse response) {
String callback = (String)request.getParameter("callback");
String jsonData = "{\"id\":\"3\", \"name\":"zhangsan", \"telephone\":"13612345678"}";//为了演示效果,json数据是写死的
String retStr = callback + "(" + jsonData + ")";
response.getWriter().print(retStr);
}