0

我有功能,我正在尝试将返回值附加到下拉列表,但它根本没有帮助。

这是我的功能

function GetNames(FROM,TO) {
 var inputObject = {};   
 inputObject.FROM = FROM;   
  inputObject.TO = TO;    
 $.ajax({   
     type: "POST",  
      url: "../WebService.asmx/getName",   
      data: {from : inputObject.FROM ,to : inputObject.TO},   
      contentType: "application/json; charset=utf-8",   
      dataType: "json",   
      async: true,   
      success: function (msg) {   
        $.each(msg.d, function (k, value) {   
            $('#ddlName').append("<option value='" + value + "'>" + value + "   </option>");   
        });   
     },   
         error: function (errMsg) {   
         alert(errMsg);   
     }   
  });   
}

我也将数据参数更改为此值,但它们没有帮助

1) data: JSON.stringify(inputObject),
2) data: {'from=' : inputObject.FROM, 'to=' : inputObject.TO},
3) data: {'from' : inputObject.FROM, 'to' : inputObject.TO},

这些变化都没有帮助。

FROM 和 TO 是我测试 webservice getName 方法及其工作正常的日期。但是这个javascript方法不断出错:函数(errMsg)和消息出现“对象:对象”

网络服务方法:

  [WebMethod]
    public List<string> getName(string from,string to)
    {
        List<string> bdnames= new List<string>();
        try
        {
            DataSet ds = null;
            SqlParameter[] sqlparams = new SqlParameter[2];


            sqlparams[0] = new SqlParameter("@FROM", SqlDbType.DateTime);
            sqlparams[0].Direction = ParameterDirection.Input;
            sqlparams[0].Value = Convert.ToDateTime(from);

            sqlparams[1] = new SqlParameter("@TO", SqlDbType.DateTime);
            sqlparams[1].Direction = ParameterDirection.Input;
            sqlparams[1].Value = Convert.ToDateTime(to);

            ds = SqlHelper.ExecuteDataset(usmHelper.ConnectionString, CommandType.StoredProcedure, "rep_getBDMs", sqlparams);

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                bdnames.Add(ds.Tables[0].Rows[i]["UUMM"].ToString());
            }
        }
        catch(Exception ex)
        {

        }
        finally
        {

        }
        return bdnames;
    }

Web 服务返回值

<?xml version="1.0" encoding="utf-8" ?> 
- <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <string>Samuel</string> 
  <string>Chetna</string> 
  <string>Chris</string> 
  <string>Mily</string> 
  <string>tuff</string> 
  <string>Prasad</string> 
  <string>Ram</string> 
  <string>Gary</string> 
  <string>MarkTaylor</string> 
  <string>Kenn</string> 
  </ArrayOfString>
4

1 回答 1

0

这是一个非常冗长的版本,但您可能会发现一些价值:

function GetNames(FROM, TO) {
    // just use the object you created for data
    var inputObject = {
        from: FROM,
        to: TO
    };
    $.ajax({
        type: "POST",
        url: "../WebService.asmx/getName",
        data: inputObject,
        contentType: "application/json",
        dataType: "json",
        async: true,
        // figure out if it has .d or not and filter for that
        dataFilter: function (data) {
            var msg;
            if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') msg = JSON.parse(data);
            else msg = jQuery.parseJSON(data);
            if (msg.hasOwnProperty('d')) return msg.d;
            else return msg;
        },
        // pass the filtered message (data returned) to a function
        success: function (msg) {
            loadOptions(msg);
        },
        // verbose error handler
        error: function (xhr, textStatus, errorThrown) {
            var errorMessage = "Ajax error: " + this.url + " : " + textStatus + " : " + errorThrown + " : " + xhr.statusText + " : " + xhr.status;
            alert(errorMessage);
            if (xhr.status != "0" || errorThrown != "abort") {
                alert(errorMessage);
            }
        }

    });
}
/* build the option list 
   This only hits the DOM once instead of your .each or  a .map etc.
*/
function loadOptions(jdata) {
    var bdnames = jdata.bdnames;
    var options = '';
    for (var i = 0; i < bdnames.length; i++) {
        options += '<option value="' + bdnames[i].string + '">' + bdnames[i].string + '</option>';
    }
    $("select#ddlName").html(options);
}
于 2013-04-17T12:41:57.157 回答