在SOAP请求中获取NULL而不是字符串值

时间:2014-08-24 00:18:30

标签: javascript jquery ajax web-services soap

我有一个移动应用程序,通过ajax与我的soap web服务进行通信。

用户必须插入2个字段才能登录(uname和密码)并按下登录名 按钮,调用javascript方法通过ajax将数据发布到Web服务。 我可以从html页面获取输入值,但是当我调试Web服务时,我看到收到的参数是null而不是用户输入的值,我不明白为什么..

function login()
{
  var uname = document.getElementById('uname').value;
  var pass  = document.getElementById('password').value;


  var soapRequest =
        '<?xml version="1.0" encoding="utf-8"?> \
        <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"\
            xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
          <soap:Body>\
            <Login xmlns="http://service.webservice.org/">\
              <Name>' + uname + '</Name>\
              <Password>' + pass + '</Password>\
            </Login>\
          </soap:Body>\
        </soap:Envelope>';  

  $.ajax({
    type : "POST",          
    url :  "http://localhost:8080/WebService/Service",          
    contentType: "text/xml",
    dataType: "xml",
    data: soapRequest,

    success : function(data, status, req, xml, xmlHttpRequest, responseXML) 
    {
        var xmlText = $(req.responseXML).find('LoginResult').text();
        alert(xmlText);
            },
    error: function () 
    { 
        alert("error");
    }

});  

1 个答案:

答案 0 :(得分:0)

答案是我们需要声明我们在soap请求中调用的方法的名称空间。

所以在您将Web服务部署到服务器(glassfish或其他东西)后,它就会创建 WSDL文件和每个方法都有命名空间。

所以soap请求应该是那样的

var soapRequest =
    '<?xml version="1.0" encoding="utf-8"?> \
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\
        xmlns:xsd="http://www.w3.org/2001/XMLSchema"\
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\
      <soap:Body>\
        <ns2:Login xmlns:ns2="http://service.webservice.org/">\
          <Name>' + uname + '</Name>\
          <Password>' + pass + '</Password>\
        </ns2:Login>\
      </soap:Body>\
    </soap:Envelope>';  

ns2是默认创建的命名空间,您可以自己设置命名空间

相关问题