【问题标题】:Getting NULL instead the string value in soap request在soap请求中获取NULL而不是字符串值
【发布时间】:2014-08-24 00:18:29
【问题描述】:

我有一个通过 ajax 与我的肥皂网络服务通信的移动应用程序。

用户必须插入 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");
    }

});  

【问题讨论】:

  • > 我可以从 html 页面获取输入值 显然不行。确认页面上确实存在具有这些 id 的元素(unamepassword)。
  • 它们在 html 页面上找到,我使用 "" 将 js 函数链接到 html 页面,因此我可以获得输入值.. 即使我在登录方法的前 2 行之后使用“alert”方法,我也可以看到输入值
  • 即使我传递的是硬编码字符串而不是 'uname' 和 'pass' 参数,我也会得到 null
  • 啊,我明白了。您在服务器上得到 null,而不是在客户端上。这更像是一个“如何用 JavaScript 做肥皂”的问题。
  • 是的,这就是问题所在.. 我不知道为什么.. 肥皂请求很好

标签: javascript jquery ajax web-services soap


【解决方案1】:

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

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

sosoap 请求应该是这样的

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是默认创建的命名空间,你可以自己设置命名空间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 2011-05-18
    • 2017-08-26
    • 1970-01-01
    相关资源
    最近更新 更多