使用Web服务结果填充变量

时间:2016-03-18 17:22:10

标签: javascript asp.net web-services

我有一个通过jquery在onClick上执行的javascript函数。在这个函数中,我调用一个Web服务“getTestConnection”,它返回一个True或False,我已经确认它正在工作,但保持返回变量undefined。

      $("#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 个答案:

答案 0 :(得分:1)

您有多个错误:

  1. 您在另一个<script type = "text/javascript">标记
  2. 中有<script>个标记
  3. 您在另一个函数中定义了一个新函数:
  4. function getDocType(json, rowcount) { $.ajax({ ..... }); function getTestConnection(json) { .... } }

    应该是

    function getDocType(json, rowcount) {
        $.ajax({
           .....
        });
    }
    function getTestConnection(json) {
       ....
    }
    
    1. 您忘记在getTestConnection函数中从AJAX调用返回数据:
    2. $.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) { .... } });