繁体   English   中英

从AJAX呼叫Web服务

[英]Call Web Service from AJAX

我已经浏览了许多关于通过AJAX调用WS的文章/教程,但是仍然无法做到这一点。 环境并不重要,但是...我在Eclipse中编写了Java类,正在GlassFish上运行它,并且可以通过soapUI到达Endpoint。

JAVA类:

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;
    }
}

终点:

localhost:8080/GlassFishTestApp/GlassFishTestAppService?wsdl

当我加载以下网页时,我仅看到“错误:”显示在中。

<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请求:

<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放置:

<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>

感谢您对此的任何投入!

得到它了!

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);
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM