繁体   English   中英

在jQuery中从ajax到php调用WSDL

[英]Calling WSDL from ajax to php in jquery

我正在开发移动网站。实际服务器在php中,所以我为此使用了Web服务。我正在使用带有完整html文件的jquery mobile。问题是我需要通过jquery中的ajax调用WSDL方法移动应用程序并从服务器获取值,这些文件是php文件。如果我运行html文件,它的执行成功部分将出现错误“ {“ location”:null}“。Kinldy帮助我执行了此操作

origin.html

<!DOCTYPE html>
<html>
    <head>
    <title>Submit a form via AJAX</title>
      <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
      <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>
      <script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js"></script>
</head>
<body>
    <script>
        function onSuccess(data, status)
        {
            data = $.trim(data);
            $("#notification").text(data);
        }

        function onError(data, status)
        {
            alert(JSON.stringify(data));
            data = $.trim(data);
            $("#notification").text(data);

            // handle an error
        }       
        $(document).ready(function() {
            $("#submit").click(function(){

                var formData = $("#callAjaxForm").serialize();

                $.ajax({
                    type: "POST",
                    url: "http://demo.XXXXX.org/test.wsdl",
                    cache: false,
                    data: formData,
                    success: onSuccess,
                    error: onError
                });
                return false;
            });
        });
    </script> 
    <!-- call ajax page -->
    <div data-role="page" id="callAjaxPage">
        <div data-role="header">
            <h1>TEST MOBILE</h1>
        </div>
     <div data-role="content">
            <form id="callAjaxForm">
                <div data-role="fieldcontain">
                    <label for="firstName">User Name</label>
                    <input type="text" name="firstName" id="firstName" value=""  />
                    <label for="firstName">Password</label>
                    <input type="password" name="password" id="password" value=""  />
                    <h3  id="notification"></h3>
                    <button data-theme="b" id="submit" type="submit">Submit</button>
                </div>
            </form>
        </div>
        <div data-role="footer">
            <h1>TEST.net</h1>
        </div>
    </div>   
    <div data-role="page" id="alert" data-add-back-btn="true">
        <div data-role="header">
            <h1>Alert!</h1>
        </div>
        <div data-role="content">
            <p>I'm alert page!</p>
        </div>
    </div>   
</div>
</body>
</html>

test.wsdl

    <?xml version="1.0"?>
<definitions name="HelloWorld" targetNamespace="urn:HelloWorld" xmlns:tns="urn:HelloWorld"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">

 <types>
    <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:doHelloResponse">
      <xsd:complexType name="doHelloResponse">
        <xsd:all>
          <xsd:element name="firstName" type="xsd:string" />
          <xsd:element name="password" type="xsd:string" />
        </xsd:all>
      </xsd:complexType>      

    </xsd:schema>   

  </types>

  <message name="doHello">
    <part name="firstName" type="xsd:string"  />
    <part name="password" type="xsd:string"  />
  </message>

  <message name="doHelloResponse">
    <part name="return" type="tns:HelloResponse" />
  </message>  

  <portType name="HelloPort">
    <operation name="doHello">
      <input message="tns:doHello" />
      <output message="tns:doHelloResponse" />
    </operation>
  </portType>

  <binding name="HelloBinding" type="tns:HelloPort">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
      <operation name="doHello">
        <soap:operation soapAction="urn:HelloAction" />
        <input>
          <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
        </input>
        <output>
          <soap:body use="encoded" namespace="urn:Hello" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />         
        </output>
      </operation>
  </binding>

  <service name="HelloService">
    <port name="HelloPort" binding="tns:HelloBinding">
      <soap:address location="http://demo.XXXXX.org/test_server.php" />
    </port>
  </service>

</definitions>

test_server.php

<?php

if(!extension_loaded("soap")){
  dl("php_soap.dll");
}

ini_set("soap.wsdl_cache_enabled","0");
$server = new SoapServer("test.wsdl");

function doHello($user,$pass){

 if($user == 'admin' && $pass == '1'){
    return 'success';
 }else{
    return 'failed';
 }
}

$server->AddFunction("doHello");
$server->handle();
?>

老实说:不要在这方面使用SOAP。 使用JSON作为沟通的基础。 JavaScript和PHP都具有解析JSON并从数组/对象创建JSON的功能。

由于您不需要深入研究项目,因此很有可能帮自己一个忙,然后切换到JSON。 jQuery有一个用于JSON-AJAX-Request的处理程序,在服务器端,您将获得普通的POST / GET-data。

暂无
暂无

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

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