用 Web 服务结果填充变量

Fill Variable with Web Service Results

本文关键字:填充 变量 结果 服务 Web      更新时间:2023-09-26

我有一个通过jquery在Click上执行的javascript函数。在这个函数中,我调用一个 Web 服务"getTestConnection",它返回一个 True 或 False,我已经确认它正在工作,但一直返回未定义的变量。

      $("#btnNext2").click(function() {
        var postData = {}; {
          postData['user'] = user;
          postData['password'] = password;
          postData['serviceurl'] = serviceurl;
          postData['datasource'] = datasource;
        };
        //Converts object to string and formats to JSON
        var json = JSON.stringify(postData);
        //connTest keeps getting returned as 'Undefined'
        var connTest = getTestConnection(json);
      });
       < script type = "text/javascript" >
        function getDocType(json, rowcount) {
          $.ajax({
            type: "POST",
            url: "http://localhost:64580/Web_Services/WebServiceLibrary.asmx/GetDocTypes",
            data: json,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data) {
              //*****************************************************************************
              //This is being called immediately after getTestConnection is executed
              //******************************************************************************         
              for (i = 0; i < data.d.length; i++) {
                $('#SelectDocType' + rowcount + '')
                  .append($("<option></option>")
                    .attr("value", data.d[i].docTypeID)
                    .text(data.d[i].docTypeName));
              }
              var firstDocTypeID = data.d[0].docTypeID;
              var jsonUnstringify = JSON.parse(json);
              var postDataNew = {}; {
                postDataNew['user'] = jsonUnstringify.user;
                postDataNew['password'] = jsonUnstringify.password;
                postDataNew['serviceurl'] = jsonUnstringify.serviceurl;
                postDataNew['datasource'] = jsonUnstringify.datasource;
                postDataNew['docTypeID'] = firstDocTypeID;
              };
              var jsonnew = JSON.stringify(postDataNew);
              getKeywords(jsonnew, rowcount);
            },
            error: function(data) {
              alert("***********Error***********" + data.responseText);
            },
            failure: function(data) {
              alert("***********Failure***********" + data.responseText);
            }
          });
          //Test Connection Web Service
          function getTestConnection(json) {
            $.ajax({
                type: "POST",
                url: "http://localhost:64580/Web_Services/WebServiceLibrary.asmx/TestConnection",
                data: json,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                if (data.d == 'True') {
                  return true;
                } else {
                  return false;
                }
              },
              error: function(data) {
                alert("***********Error***********" + data.responseText);
              },
              failure: function(data) {
                alert("***********Failure***********" + data.responseText);
              }
            });
        }
       < /script>

您有多个错误:

  1. 您在另一个<script>标签中有一个<script type = "text/javascript">标签
  2. 在另一个函数中定义一个新函数:

function getDocType(json, rowcount) { $.ajax({ ..... }); function getTestConnection(json) { .... } }

应该是

function getDocType(json, rowcount) {
    $.ajax({
       .....
    });
}
function getTestConnection(json) {
   ....
}
  1. 您忘记在getTestConnection函数中从AJAX调用中获取返回的数据:

$.ajax({ type: "POST", url: "http://localhost...", data: json, contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { if (data.d == 'True') { return true; } else { return false; } }, error: function(data) { .... } });