2017-11-16 5 views
0

私は、jqueryからwebmethodに値を渡そうとしています。しかし1つのパスの値webmethodが開始されないとき。そして、渡された値のコードを削除すると、正しく動作するようになります。問題は何ですか? JSコード。Webmethodが動作しなくなる

$(function() { 
    $.ajax({ 
     type: "POST", 
     url: "ContryCityDDL.aspx/GetCountry", 
     data: '{}', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (r) { 
      var ddlcontry = $("[id*=ddlcontry]"); 
      ddlcontry.empty().append('<option selected="selected" value="0">Please select</option>'); 
      $.each(r.d, function() { 
       ddlcontry.append($("<option></option>").val(this['Value']).html(this['Text'])); 
      }); 
     } 
    }); 

    $("#ddlcontry").change(function() { 
     alert($('option:selected', $(this)).val()); 
     var countyId = $(this).val(); 

     $("#HiddenField1").val($('option:selected', $(this)).val()); 


     $.ajax({ 
      type: "POST", 
      url: "ContryCityDDL.aspx/GetCity&countryId='"+countryId+"'", 
      data: '{}', 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: function (r) { 
       var ddlcity = $("[id*=ddlcity]"); 
       ddlcity.empty().append('<option selected="selected" value="0">Please select</option>'); 
       $.each(r.d, function() { 
        ddlcity.append($("<option></option>").val(this['Value']).html(this['Text'])); 
       }); 
      } 
     }); 
    }); 
}); 

C#

[WebMethod] 
public static List<ListItem> GetCity(string countryId) 
{ 

    string constrng; 
      string query = "Select CityId,CityName From City"; 
    //string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 

    constrng = "Data Source=(LocalDB)\\v11.0;AttachDbFilename='C:\\Users\\Abdul Basit Mehmood\\Desktop\\DropdownAJAX\\DropdownAJAX\\App_Data\\ContryCity.mdf';Integrated Security=True"; 

    using (SqlConnection con = new SqlConnection(constrng)) 
    { 
     using (SqlCommand cmd = new SqlCommand(query)) 
     { 
      List<ListItem> customers = new List<ListItem>(); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = con; 
      con.Open(); 
      using (SqlDataReader sdr = cmd.ExecuteReader()) 
      { 
       while (sdr.Read()) 
       { 
        customers.Add(new ListItem 
        { 
         Value = sdr["CityId"].ToString(), 
         Text = sdr["CityName"].ToString() 
        }); 
       } 
      } 
      con.Close(); 
      return customers; 
     } 
    } 
} 

iは、C#コードから「列countryId'及び() `VAR countyId = $(この).valを取り除きます。コードが正しく実行JSから& countryId =「『+ countryId +』」」。

+0

、ddlcontry変更機能でこれを試してみてくださいGetCity(string countryId)が機能しない理由関数内の引数を読みたいときにissuとは何ですか? jsの変更は何の違いもありません –

答えて

2

あなたのAJAX呼び出しと、その場合には使用?文字ではなく&

"ContryCityDDL.aspx/GetCity?countryId='" 

などからクエリ文字列として ContryIdを渡したいです
+0

まだ動作していません! –

+0

コンパイラはエラーを表示していませんが、GetCity()関数が正しく動作していますが、GetCity(string countryId)に変更すると実行されません。私は選択された国に基づいて都市のドロップダウンリストを設定するためにcountryIdが必要です。 –

+0

@AbdulBasitMehmood、コンパイラはクライアントコードのエラーを表示しません... JSコード – Rahul

0

誰かが私に言うことができるしてください

$("#ddlcontry").change(function() { 
      alert($('option:selected', $(this)).val()); 
      var countryId = $(this).val(); 

      $("#HiddenField1").val($('option:selected', $(this)).val()); 


      $.ajax({ 
       type: "POST", 
       url: "ContryCityDDL.aspx/GetCity", 
       data: "{'countryId':'" + countryId + "'}", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (r) { 
        var ddlcity = $("[id*=ddlcity]"); 
        ddlcity.empty().append('<option selected="selected" value="0">Please select</option>'); 
        $.each(r.d, function() { 
         ddlcity.append($("<option></option>").val(this['Value']).html(this['Text'])); 
        }); 
       } 
      }); 
     }); 

はこれが働いている願っています。

関連する問題