2013-04-17 51 views
0

我有功能,我試圖追加返回值到下拉,但它根本沒有幫助。無法綁定jQuery的下拉菜單

這裏是我的功能

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)和消息來了 「對象:對象」

Web服務方法:

[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> 
+0

它的AV如果您在執行維修呼叫時發現錯誤,請通過網絡服務 – dawncode

+0

粘貼網絡服務方法代碼 –

+0

檢查瀏覽器控制檯。很可能你應該看到一些訪問原始錯誤。 – Watt

回答

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); 
} 
+0

注意我不確定是否需要'var bdnames = jdata.bdnames;''''或'var bdnames = jdata;'馬上就好了,因爲我的大腦此時正在霧中:) –

+0

嗨,我已經改變LoadProcedure(msg); loadOptions(msg);我嘗試了兩種方式bdnames = jdata或bdnames = jdata.bdnames;它不斷給我這個錯誤: Ajax錯誤:../ WebService.asmx/getName:error:內部服務器錯誤:內部服務器錯誤:500 – dawncode

+0

我得到另一個simil ()函數GetH1Years(){ $ .ajax({type:「POST」, url:「../WebService.asmx/getYears」, 數據:「{}」, contentType:「application/json; charset = utf-8「, dataType:」json「, async:true, 成功:函數(msg){.gfg},函數(k,value){('#ddlyears' ).append( 「<選項值= '」 +值+ 「'>」 +值+「」); });} , 錯誤:功能(jqXhr){ errorCaller(jqXhr); } });}' – dawncode