drygauost253590142 2012-09-07 19:01
浏览 37
已采纳

使用jQuery调用PHP SoapServer函数

I am attempting to use PHP 5.2's SoapServer functionality. I setup soapserv.php as shown below. But I am lost as to how I can call the 'add' function with a language other than PHP (without WSDL). I want to make calls to it with AJAX (jQuery). Any help would be greatly appreciated!

<?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();
?>

And my JavaScript looks like this (tried several ways though).

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);
            });
  • 写回答

1条回答 默认 最新

  • 普通网友 2012-09-07 20:38
    关注

    Got it... Just had to change the post data to a soap envelope!

            var request = "<?xml version=\"1.0\"?>
    ";
            request += "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">
    ";
            request += "<soap:Body xmlns:m=\"http://example.com/projects/php/soapserv\">
    ";
            request += "<m:add>
    ";
            request += "<m:a>1</m:a>
    ";
            request += "<m:b>2</m:b>
    ";
            request += "</m:add>
    ";
            request += "</soap:Body>
    ";
            request += "</soap:Envelope>";
    
            var req = $.ajax({
               type: "POST",
               url: "http://example.com/projects/php/soapserv",
               data: request,
               cache: false
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频