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