0

我正在尝试使用 PHP 5.2 的 SoapServer 功能。我设置了soapserv.php,如下所示。但是我不知道如何用 PHP 以外的语言(没有 WSDL)调用“添加”函数。我想用 AJAX (jQuery) 调用它。任何帮助将不胜感激!

<?php
    function add($a, $b) {
      return $a + $b;
    }

  $soap = new SoapServer(
    null,
    array('uri' => 'http://example.com/projects/php/soapserv/')
  );
  $soap->addFunction("add");
  $soap->handle();
?>

我的 JavaScript 看起来像这样(虽然尝试了几种方法)。

var req = $.post("http://example.com/projects/php/soapserv", {"add":{a: 1, b: 2}});

            req.done(function(msg){
                document.write(msg.responseText);
            });

            req.fail(function(msg){
                document.write(msg.responseText);
            });
4

1 回答 1

0

明白了...只需将帖子数据更改为肥皂信封!

        var request = "<?xml version=\"1.0\"?>\n";
        request += "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">\n";
        request += "<soap:Body xmlns:m=\"http://example.com/projects/php/soapserv\">\n";
        request += "<m:add>\n";
        request += "<m:a>1</m:a>\n";
        request += "<m:b>2</m:b>\n";
        request += "</m:add>\n";
        request += "</soap:Body>\n";
        request += "</soap:Envelope>";

        var req = $.ajax({
           type: "POST",
           url: "http://example.com/projects/php/soapserv",
           data: request,
           cache: false
        });
于 2012-09-07T20:38:34.473 回答