weixin_33720186 2014-01-31 16:13 采纳率: 0%
浏览 34

从AJAX调用Web服务

I've looked through a number of posts/tutorials on calling a WS via AJAX, but am still not able to do this. Not that the environment matters but... I wrote my Java Class in Eclipse, I'm running it on GlassFish, and I'm able to hit the Endpoint via soapUI.

JAVA Class:

package com.tester.gf;

import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;

@WebService
public class GlassFishTestApp {

    public List<String> getBrands() {
        List<String> brands = new ArrayList<>();
        brands.add("Chevrolet");
        brands.add("Dodge");
        brands.add("Ford");
        return brands;
    }
}

Endpoint:

localhost:8080/GlassFishTestApp/GlassFishTestAppService?wsdl

When I load the following Web Page, I only see "Error: " displayed in the .

<html>
    <head>
        <title>SOAP WS Test</title>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script>
            $(document).ready(function () {
                $.ajax(
                    type: 'get',
                    url: 'http://localhost:8080/GlassFishTestApp/GlassFishTestAppService',
                    success: function (data) {
                        $('#results').text(data);
                    },
                    error: function (request, status, error) {
                        $('#results').text('Error: ' + request.responseText);
                    }
                });
            });
        </script>
    </head>
    <body>
        <div id="results"></div>
    </body>
<html>

soapUI Request:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gf="http://gf.tester.com/">
    <soapenv:Header/>
        <soapenv:Body>
            <gf:getBrands/>
        </soapenv:Body>
</soapenv:Envelope>

soapUI Respose:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:getBrandsResponse xmlns:ns2="http://gf.tester.com/">
             <return>Chevrolet</return>
             <return>Dodge</return>
             <return>Ford</return>
        </ns2:getBrandsResponse>
    </S:Body>
</S:Envelope>

Thanks for any input on this!

  • 写回答

1条回答 默认 最新

  • weixin_33701294 2014-01-31 17:43
    关注

    Got it!

    var soapMessage =
        '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gf="http://gf.tester.com/">' +
            '<soapenv:Header/>' +
                '<soapenv:Body>' +
                    '<gf:getBrands/>' +
                '</soapenv:Body>' +
        '</soapenv:Envelope>';
    
    $.ajax({
        url: "/GlassFishTestApp/GlassFishTestAppService",
        type: "POST",
        dataType: "xml",
        contentType: "text/xml; charset=\"utf-8\"",
        headers: {
            SOAPAction: "/GlassFishTestApp/GlassFishTestAppService/getBrands"
        },
        data: soapMessage,
        success: function(data) {
            $('#results').text(data);
        },
        error: function (request, status, error) {
            $('#results').text('Error: ' + error);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路