海星派笔记 2015-05-29 01:28 采纳率: 0%
浏览 2210

webService 使用Ajax提交的问题

ajax请求代码:

 <script>
    var xhr = new XMLHttpRequest();
    function wsConnection(){
        //创建XMLHttp的对象

        //服务的地址
        var urlMsg = 'http://192.168.1.174:6789/hello';

        //请求体
        var soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://score.it.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema'+        'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><q0:sayHello> <arg0>aaa</arg0> <arg1>123</arg1></q0:sayHello></soapenv:Body> </soapenv:Envelope>';

        //打开连接-true :表示为异步请求
        xhr.open('POST',urlMsg,true);     
        xhr.crossDomain=true;   

        //重新设置头信息
        xhr.setRequestHeader("Content-Type","text/xml;charset=utf-8");   

        //设置回调函数
        xhr.onreadystatechange=_back;

        //发送请求
        xhr.send(soap);
    }

    function _back(){
        if(xhr.readyState==4){
            if(xhr.status==200){
                alert('成功响应');
                var ret = xhr.responseXML;
                var msg = ret.getElementsByTagName("return")[0];
                alert(msg.text)
            }
        }
    }
</script>
<body>
    <input type="button" value="发送请求" onclick="wsConnection()"/>
    <input type="text" id="response"/>
</body>

主程序与控制台输出代码:

 @WebService
public class HelloService {

    public String sayHello(String name,int num){
        System.out.println("客户请求来了..................");
        return "Hello "+name;
    }

    //发布时,此方法不显示
    @WebMethod(exclude=true)
    public String sayHi(String name){
        return "Hi "+name;
    }

    public static void main(String[] args) {
        /**
         * 参数1:服务发布地址
         * 参数2:服务实现者
         */
        Endpoint.publish("http://192.168.1.174:6789/hello", new HelloService());
        System.out.println("我是由主线程执行");
    }

}

控制台:
五月 29, 2015 9:30:06 上午 com.sun.xml.ws.transport.http.server.WSHttpHandler handleExchange
警告: 无法处理 HTTP 方法: OPTIONS

  • 写回答

2条回答

  • 海星派笔记 2015-05-29 01:31
    关注

    补充:ajax提交后所示:![图片说明](https://img-ask.csdn.net/upload/201505/29/1432863066_123052.png)图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)