简体   繁体   中英

Calling WSDL from ajax to php in jquery

I'm developing mobile website.Actual server is in php.So i've used web services for that.I'm using jquery mobile with full of html files.The problem is i need to call the WSDL method via ajax in my jquery mobile application and get the value from server which are the php files.If i run the html file its executing success part with the error "{"location":null}".Kinldy help me to do this

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

To be honest: dont use SOAP on this side. use JSON as a base of communication. Both, JavaScript and PHP do have functions to parse JSON and create JSON from arrays/objects.

Since you are not to deep in the project, it seams quite possible to do yourself a favour and switch to JSON. jQuery has a handler for JSON-AJAX-Request and on the serverside you will get plain POST/GET-data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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