码迷,mamicode.com
首页 > 编程语言 > 详细

Java 调用 php接口(Ajax)(二)

时间:2018-01-02 16:47:00      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:2.x   active   dex   mime   pos   rri   window   overflow   erp   

由于项目里面需要用到Java调用PHP的充值接口,所以学习了一下,以下这个Demo是个小小的例子,写下来做个笔记>

 

jsp页面:

 

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"
 contentType="text/html; charset=GBK"%>

<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <base href="<%=basePath%>" />
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  <meta http-equiv="x-ua-compatible" content="ie=7" />

  <title>Lost Crown</title>
  <link rel="icon" href="favicon.ico" />
  <link rel="shortcut icon" href="favicon.ico" />
<script type="text/javascript">
function getXmlHttp(){
    var http_request = false;
    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType(‘text/xml‘);
 }
   }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e) {
            }
        }
    }
    if (!http_request){
        alert("不能创建对象!");
        return false;
    }
    return http_request;
}

function result(){
    if(req.readyState == 4 && req.status == 200){
     var names =  req.responseText;
     alert("Hello:"+names);
    }    
}

function checkTest(){
 var name = document.getElementById("name").value;
    var ul = "http://localhost:8011/TestAjax/index.php?name=" + name;
    req = getXmlHttp();
    req.onreadystatechange = result;
    req.open("POST",ul,true);
    req.setRequestHeader(‘If-Modified-Since‘, ‘0‘);
    req.send(null);
}
</script>
</head>
 <body> 
  <center>
   <table width="300" border=1>
   <tr align="center">
    <td colspan="2">
     It‘s A Ajax Test
    </td>
   </tr>
   <tr align="center">
    <td>
     用户名
    </td>
    <td>
     <input type="text" name="name" id="name" value="Jerry"/>
    </td>
   </tr>
   <tr align="center">
    <td colspan="2">
     <input type="button" name="submit" id="submit" value="提交" onclick="checkTest();"/>
    </td>
   </tr>
  </table>
  </center>
 </body>
</html>

 


php页面:

 

<?php

$username = $_REQUEST[‘name‘];

echo($username);

?>

 

Java 调用 php接口(Ajax)(二)

标签:2.x   active   dex   mime   pos   rri   window   overflow   erp   

原文地址:https://www.cnblogs.com/MaxElephant/p/8177469.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!